﻿#include "pch.h"

AnalExecutor::AnalExecutor()
{
    memset(this, 0, sizeof(AnalExecutor));
}

char *AnalExecutor::getErrMsg()
{
    return (char *)this->analInfo.errMsg;
}

void AnalExecutor::printSelf(char *captionUpper, int32_t deep)
{
    char caption[1024];
    sprintf(caption, "%s:%s", captionUpper, "AnalExecutor");
    print4x(this, sizeof(AnalExecutor), caption);
    if (--deep >= 0)
    {
        if (this->pTsmpBag)
        {
            this->pTsmpBag->printSelf(caption, deep);
        }
        if (this->pIParaWrapper)
        {
            this->pIParaWrapper->printSelf(caption, deep);
        }
        if (this->pTprgInfWrapper)
        {
            this->pTprgInfWrapper->printSelf(caption, deep);
        }
        if (this->pResInfWrapper)
        {
            this->pResInfWrapper->printSelf(caption, deep);
        }
        if (this->pCTickmarkManager)
        {
            this->pCTickmarkManager->printSelf(caption, deep);
        }
        if (this->pBuffer10Wrapper)
        {
            this->pBuffer10Wrapper->printSelf(caption, deep);
        }
        if (this->pEventManager)
        {
            this->pEventManager->printSelf(caption, deep);
        }
        if (this->pCaculator)
        {
            this->pCaculator->printSelf(caption, deep);
        }
        if (this->pConcCaculator)
        {
            this->pConcCaculator->printSelf(caption, deep);
        }
    }
    if (this->pRawBagBuffer)
    {
        sprintf(caption, "%s:%s", captionUpper, "AnalExecutor.pRawBagBuffer");
        print4x(this->pRawBagBuffer, this->pTsmpBag != NULL ? this->pTsmpBag->getRawBagLen() * 4 : 4, caption);
    }
}

// sub_1000CA73
AnalExecutor *AnalExecutor::init(CTsmpBag *pTsmpBag, CIParaWrapper *pIParaWrapper, CTprgInfWrapper *pTprgInfWrapper,
                                 CResInfWrapper *pResInfWrapper, CEParaWrapper *pEParaWrapper,
                                 CGrpInfWrapper *pGrpInfWrapper, CIdtInfWrapper *pIdtInfWrapper,
                                 CStdInfManager *pStdInfManager)
{
    float *rawbagFreq;
    int32_t len;

    CTickmarkManager *pTickmarkManager;
    CBuffer10Wrapper *pBuffer10Wrapper;
    CEventManager *pEventManager;
    CResultCaculator *pCaculator;
    CConcCaculator *pConcCaculator;

    this->pTsmpBag = (CTsmpBag *)pTsmpBag;
    this->pIParaWrapper = pIParaWrapper;
    this->pTprgInfWrapper = pTprgInfWrapper;
    this->pResInfWrapper = pResInfWrapper;
    len = this->pTsmpBag->getRawBagLen();
    //// *(DWORD *)(this + 164) = operator new(4 * v22);
    this->pRawBagBuffer = new int32_t[len];
    memset(this->pRawBagBuffer, 0, sizeof(int32_t) * len);

    pTickmarkManager = new CTickmarkManager();
    if (pTickmarkManager)
    {
        pTickmarkManager = pTickmarkManager->init(32);
    }
    this->pCTickmarkManager = pTickmarkManager;

    pBuffer10Wrapper = new CBuffer10Wrapper();
    if (pBuffer10Wrapper)
    {
        pBuffer10Wrapper = pBuffer10Wrapper->init(10);
    }
    this->pBuffer10Wrapper = pBuffer10Wrapper;

    pEventManager = new CEventManager();
    if (pEventManager)
    {
        rawbagFreq = this->pTsmpBag->getRawBagFreq();
        pEventManager = pEventManager->init(this->pIParaWrapper, this->pTprgInfWrapper, rawbagFreq);
    }
    this->pEventManager = pEventManager;

    pCaculator = new CResultCaculator();
    if (pCaculator)
    {
        pCaculator = pCaculator->init(this->pCTickmarkManager, this->pBuffer10Wrapper, this->pEventManager,
                                      this->pResInfWrapper, &this->analInfo, this->pRawBagBuffer, this);
    }
    this->pCaculator = pCaculator;

    pConcCaculator = new CConcCaculator();
    if (pConcCaculator)
    {
        pConcCaculator =
            pConcCaculator->init(this->pResInfWrapper, pEParaWrapper, pGrpInfWrapper, pIdtInfWrapper, pStdInfManager);
    }
    this->pConcCaculator = pConcCaculator;

    if (pIdtInfWrapper)
    {
        pIdtInfWrapper->setBased(((LPCTDLLEPARA)(pEParaWrapper->head))->based);
    }
    this->initData();

    return this;
}

// sub_1000CE41
void AnalExecutor::initData()
{
    int32_t length;
    float initTime_;
    int32_t i;
    int32_t num;

    this->rawbagOffset = 0;
    this->absoluteDrop = 0;
    initTime_ = pIParaWrapper->getIParaInitTime();
    this->evCount = pEventManager->getVibrateCount(initTime_);
    this->savedRawbagDrop = 0;
    this->loopCounter2 = 0;
    this->loopCounter0 = 0;
    this->analInfo.code = ANAL_OK;
    memset(&this->analInfo.errMsg, 0, 0x400u);
    this->analInfo.status = 0;
    this->togglerAutoNegative = false;
    this->toggle7 = false;
    this->clearItem8();
    this->clearItem16();
    this->clearItem24();
    length = this->pTsmpBag->getRawBagLen();
    memset((LPVOID)pRawBagBuffer, 0, 4 * length);
    if (this->pTsmpBag)
    {
        num = this->pTsmpBag->getRawBagTime();
        if (num > 0)
        {
            for (i = 0; i < std::min(num, length); ++i)
            {
                pRawBagBuffer[i] = this->pTsmpBag->getRawBagDrop(i);
            }
        }
    }
}

// sub_1000CDE7
void *AnalExecutor::clearItem8()
{
    return memset((void *)&(this->item8), 0, sizeof(ItemValue));
}

// sub_1000CE05
void *AnalExecutor::clearItem16()
{
    return memset((void *)&(this->item16), 0, sizeof(ItemValue));
}

// sub_1000CE23
void *AnalExecutor::clearItem24()
{
    return memset((void *)&(this->item24), 0, sizeof(ItemValue));
}

// sub_1001D5F0
void *AnalExecutor::close(char flag)
{
    this->close();
    if (flag & 1)
    {
        delete this;
        return NULL;
    }
    return this;
}

// sub_1000CD08
void AnalExecutor::close()
{
    CResultCaculator *pCaculator;
    CBuffer10Wrapper *pBuffer10Wrapper;
    CTickmarkManager *pTickmarkManager;

    pTickmarkManager = this->pCTickmarkManager;
    if (pTickmarkManager)
    {
        pTickmarkManager->close(1);
    }
    pBuffer10Wrapper = this->pBuffer10Wrapper;
    if (pBuffer10Wrapper)
    {
        pBuffer10Wrapper->close(1);
    }
    delete[] this->pEventManager;
    pCaculator = this->pCaculator;
    if (pCaculator)
    {
        pCaculator->close(1);
    }
    delete[] this->pConcCaculator;
    delete[] this->pRawBagBuffer;
}

// sub_1000CFFE
int32_t AnalExecutor::start()
{
    this->start_();
    return true;
}

// sub_1000CF89
int32_t AnalExecutor::start_()
{
    this->initData();
    this->pCTickmarkManager->clearHead();
    this->pBuffer10Wrapper->clean();
    pTprgInfWrapper->clean();
    this->pResInfWrapper->clearHead();
    pEventManager->init_();
    pCaculator->clean();
    return pConcCaculator->clean();
}

// sub_1000D224
int32_t AnalExecutor::stop()
{
    int32_t result;

    result = this->stop_(0);
    this->pEventManager->clear();
    return result;
}

// sub_1001D680
CIParaWrapper *AnalExecutor::setStopTime(float stopTime)
{
    return this->pIParaWrapper->setStopTime(stopTime);
}

// sub_1000F613
CResultCaculator *AnalExecutor::initRawBag(int32_t *)
{
    int32_t len;
    int32_t i;

    if (this->pRawBagBuffer)
    {
        operator delete(this->pRawBagBuffer);
    }
    len = this->pTsmpBag->getRawBagLen();
    this->pRawBagBuffer = new int32_t[len];
    memset(this->pRawBagBuffer, 0, 4 * len);
    for (i = 0; i < this->rawbagOffset; ++i)
    {
        this->pRawBagBuffer[i] = this->pTsmpBag->getRawBagDrop(i);
    }
    return this->pCaculator->setRawBag(this->pRawBagBuffer);
}

// sub_1001B470
int32_t AnalExecutor::isItem8HsPositive()
{
    return this->item8.hs > 0;
}

// sub_1001BCA0
int32_t AnalExecutor::isType2Lose1D()
{
    return !this->pBuffer10Wrapper->checkType2Counter(this->pEventManager->topOrBottom) && !this->pCaculator->isLocalDiffHExtruded1D();
}

// sub_1000F0C1
int32_t AnalExecutor::setItem8AndCalcResult(int32_t flag)
{
    int32_t result;
    float slope;
    int32_t height1;
    int32_t height0;

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return 1;
    }
    if (this->item8.flag)
    {
        return 1;
    }
    height0 = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(0);
    height1 = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(1);
    if (this->isType2Lose1D())
    {
        this->item24.hs = this->item8.hs;
        this->item8.flag = true;
    }
    else if (height0 < this->item8.hs && height1 < this->item8.hs)
    {
        slope = this->pEventManager->getNormalizeSlope();
        if (this->pBuffer10Wrapper->setCalcUnit1(this->pCaculator->pTDLLItem, slope))
        {
            this->item8.tr = (float)this->pBuffer10Wrapper->getUnit1TimeEnd();
            this->item8.hr = (float)this->pBuffer10Wrapper->getUnit1Height();
            this->item8.arear = this->pBuffer10Wrapper->getUnit1Area();
        }
    }
    if (this->item24.hs <= 0 && height0 < this->item8.hs && !flag)
    {
        return 1;
    }
    if (this->item24.hs <= 0 && this->item8.hr <= 0.0)
    {
        this->item8.tr = (float)this->pBuffer10Wrapper->getBuffer10Time(1);
        this->item8.hr = (float)this->pBuffer10Wrapper->getBuffer10Height(1);
        this->item8.arear = this->pBuffer10Wrapper->getBuffer10Area(1);
        this->item8.flag = true;
    }
    this->pBuffer10Wrapper->cleanCalcUnit1();
    if (this->item24.hs <= 0)
    {
        slope = this->pEventManager->getSlope();
        if (this->checkAreaDeviation(this->pCaculator->pTDLLItem, &this->item8, slope))
        {
            this->pCaculator->setPeakEnd((int32_t)this->item8.tr, (int32_t)this->item8.hr, this->item8.arear);
            this->pCaculator->processIndexAndTickmark(0);
            this->pCaculator->calcResultAndSetBaseIdx();
            this->pCaculator->setPeakStart((int32_t)this->item8.tr, (int32_t)this->item8.hr, this->item8.arear);
            this->pCaculator->calcTypeCode(0);
            this->toggle7 = true;
        }
        this->clearItem8();
        result = 1;
    }
    else
    {
        this->clearItem8();
        result = 2;
    }
    return result;
}

// sub_1000D016
int32_t AnalExecutor::stop_(int32_t idx)
{
    int32_t flag;

    if (this->pCaculator->checkResInfoNum(-1))
    {
        return false;
    }
    if (this->rawbagOffset >= this->pTsmpBag->getRawBagTime())
    {
        this->rawbagOffset = this->pTsmpBag->getRawBagTime() - 1;
    }
    if (this->isItem8HsPositive())
    {
        this->setItem8AndCalcResult(1);
    }
    if (!this->pCaculator->finishFlag)
    {
        flag = this->pCaculator->getTickmarkStatus() || this->pCaculator->pTDLLItem->Arear > 0.0;
        if (this->pCaculator->pTDLLItem->Areae <= 0.0 || this->pCaculator->pTDLLItem->te <= 0)
        {
            if (!flag)
            {
                if (this->pCaculator->baseIdx > 0)
                {
                    this->pCaculator->processIndexAndTickmark(-1);
                }
                goto LABEL_27;
            }
            this->pCaculator->finishEndValueAccordingBuffer10(0, idx, 0);
            if (!this->pCaculator->isTHValueInitialized(0) && (this->pCaculator->isDriftOrSlopeExceedDeltaH() || (!this->pEventManager->topOrBottom && this->analInfo.status != 3)))
            {
                this->pCaculator->goPrevAfterClear();
            }
        }
        this->pCaculator->processIndexAndTickmark(0);
    }
LABEL_27:
    if (this->pCaculator->baseIdx > 0)
    {
        this->calcResultAndSetMetric();
    }
    this->analInfo.status = 0;
    return true;
}

// sub_1000D24F
int32_t AnalExecutor::calcResultW50etc(char flag)
{
    int32_t area;
    int32_t *pNum;
    int32_t remain;
    int32_t height;
    int32_t *pRawBagBuffer;
    float freq;
    TDLLResultListItem *pHead;

    height = this->pEventManager->minHeight;
    area = this->pEventManager->minArea;
    pHead = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    pNum = this->pResInfWrapper->getNumPointer();
    remain = this->pResInfWrapper->filterOutInvalidItem(pHead, *pNum - 1, height, area, flag, 0);
    *pNum = remain + 1;
    pRawBagBuffer = this->pCaculator->pRawBagBuffer;
    freq = (float)this->pEventManager->getRawbagFreqValue();
    return this->pResInfWrapper->calcW50etcAll(pHead, remain, pRawBagBuffer, freq);
}

// sub_1000F6E7
int32_t AnalExecutor::handlePrgItemEvent()
{
    int32_t event;
    float te;
    int32_t eCount;
    float ts;
    int32_t i;
    int32_t num;

    TDLLResultListItem *pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();

    if (!this->pIParaWrapper->getTprgUsed())
    {
        return false;
    }
    eCount = this->pTprgInfWrapper->eCount;
    num = this->pTprgInfWrapper->getNum();
    if (!num)
    {
        return false;
    }
    for (i = eCount; i < num; ++i)
    {
        event = this->pTprgInfWrapper->getPrgItemEventAndTsTe(i, &ts, &te);
        if (event != -1)
        {
            switch (event)
            {
            case PEAK_START:
                this->handlePeakStart(ts, 0);
                break;
            case PEAK_END:
                this->handlePeakEnd(ts, 0);
                break;
            case PEAK_MOVE_SPLIT:
                this->handlePeakMoveSplit(ts, 0);
                break;
            case PEAK_ADD_SPLIT:
                if (ts <= 0.0001)
                {
                    this->handlePeakAddSplit(te, 0);
                }
                else
                {
                    this->handlePeakAddSplit(ts, 0);
                }
                break;
            case PEAK_DEL_SPLIT:
                this->handlePeakDelSplit(ts, 0);
                break;
            case PEAK_ADD_ONLYONE:
                this->handlePeakAddOnlyOne(ts, te, 0);
                break;
            case PEAK_DELETE:
                this->handlePeakDelete(ts, te, 0);
                break;
            case BASE_VALLEY:
                this->handleBaseValley(ts, te, 0);
                break;
            case BASE_FRONT_TANGENT:
                this->handleBaseFrontTangent(ts, te, 0);
                break;
            case BASE_TOGETHER_:
                this->handleBaseTogether_(ts, te, 0);
                break;
            case BASE_TAIL_TANGENT_:
                this->handleBaseTailTangent(ts, te, 0);
                break;
            default:
                continue;
            }
        }
    }
    return true;
}

// sub_1001C6C0
int32_t AnalExecutor::setConcMetric()
{
    TDLLMETRIC *metric; // eax

    // metric = (TDLLMETRIC*)this->pResInfWrapper->head;
    metric = this->pTsmpBag->metric;
    return this->pConcCaculator->setMetric(metric, 0);
}

// sub_1000EF62
int32_t AnalExecutor::checkAreaDeviation(TDLLResultListItem *pRes, ItemValue *item, float slope)
{
    double itemDrift;
    double resDrift;

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return false;
    }
    if (item->hr - pRes->hr < 10.0 || 5.0 * slope > item->hr - pRes->hr)
    {
        return false;
    }
    resDrift = (pRes->tr - pRes->ts) * pRes->hs - (pRes->Arear - pRes->Areas);
    itemDrift = (item->tr - pRes->tr) * item->hr - (item->arear - pRes->Arear);
    return (4.0 * itemDrift >= resDrift || (item->tr - pRes->tr) * 4.0 >= pRes->tr - pRes->ts) && (4.0 * resDrift >= itemDrift || (pRes->tr - pRes->ts) * 4.0 >= item->tr - pRes->tr);
}

// sub_1000DD0E
int32_t AnalExecutor::calcResultAndSetMetric()
{
    TDLLMETRIC *metric;
    int32_t result;

    result = this->pCaculator->calcResultAndSetBaseIdx();
    if (result)
    {
        metric = this->pTsmpBag->metric;
        this->pConcCaculator->setMetric(metric, 1);
    }
    return result;
}

// sub_1000FC95
int32_t AnalExecutor::handlePeakStart(float time, int32_t flag)
{
    int32_t result;
    int32_t callResult;
    int32_t k;
    int32_t vibrateCount;
    int32_t num;
    int32_t i;
    int32_t j;
    int32_t te = 0;
    TDLLResultListItem *pRes;
    float freq;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();

    if (!pRes || !num || !this->pRawBagBuffer || !(int32_t)freq)
    {
        return 0;
    }
    vibrateCount = (int32_t)(time * 60.0 * freq);
    for (i = 0;
         (i < num) && ((pRes[i].tr <= 0.0) || (vibrateCount >= pRes[i].tr && (pRes[i].ts != pRes[i].tr || vibrateCount >= pRes[i].te)) || (i && vibrateCount < pRes[i - 1].te && (pRes[i - 1].te != pRes[i - 1].tr || vibrateCount < pRes[i - 1].ts) && (pRes[i - 1].tr <= 0 || vibrateCount <= pRes[i - 1].tr)));
         ++i)
    {
        ;
    }
    if (i == num)
    {
        this->setError(flag, aNoFindTheGoalP_CN, aNoFindTheGoalP_EN);
        result = false;
    }
    else if (typeCode1Without84_116(&pRes[i], 0))
    {
        if (pRes[i].fts <= 0.0 || vibrateCount >= pRes[i].fts)
        {
            if (flag)
            {
                result = true;
            }
            else
            {
                if (pRes[i].TypeCode[1] == C80 && i > 0)
                {
                    pRes[i].TypeCode[1] = C66;
                    pRes[i - 1].TypeCode[2] = C66;
                }
                if ((pRes[i].TypeCode[0] & X7F) != C84 && (pRes[i].TypeCode[0] & X7F) != C116)
                {
                    for (j = i; j < num; ++j)
                    {
                        if ((pRes[j].TypeCode[0] & X7F) != C84 && (pRes[i].TypeCode[0] & X7F) != C116 && typeCode2Without84_116(&pRes[j], 0) && pRes[i].tr > 0)
                        {
                            te = pRes[j].te;
                            break;
                        }
                    }
                }
                else
                {
                    for (j = i; j < num; ++j)
                    {
                        if (pRes[j].TypeCode[0] == pRes[i].TypeCode[0] && typeCode2Without84_116(&pRes[j], 0) && pRes[i].tr > 0)
                        {
                            te = pRes[j].te;
                            break;
                        }
                    }
                }
                if (j == num)
                {
                    j = num - 1;
                    te = pRes[num - 1].te;
                }
                callResult = checkTrPeak(this->pRawBagBuffer, pRes, i, j, (float)vibrateCount, (float)te);
                if (!callResult && (pRes[i].TypeCode[0] & X7F) == C116 && typeCode1Without84_116(&pRes[i], 0) && (i > 0 || typeCode2Without84_116(&pRes[i - 1], 2)))
                {
                    for (k = i; k < num - 1 && !isPeakType36_115(&pRes[k]); ++k)
                    {
                        ;
                    }
                    i = k;
                    for (j = k; j < num - 1 && !typeCode2Without84_116(&pRes[j], 1); ++j)
                    {
                        ;
                    }
                    te = pRes[j].te;
                    callResult = checkTrPeak(this->pRawBagBuffer, pRes, k, j, (float)vibrateCount, (float)te);
                }
                if (callResult)
                {
                    this->calcAndSetMetric(i, j, 0);
                }
                if (i > 0 && vibrateCount < pRes[i - 1].te)
                {
                    this->handlePeakEnd(time, (184 * i) & 0xff00);
                }
                result = callResult;
            }
        }
        else
        {
            this->setError(flag, aThePeakStartCa_CN, aThePeakStartCa_EN);
            result = false;
        }
    }
    else
    {
        this->setError(flag, aThePeakStartMu_CN, aThePeakStartMu_EN);
        result = false;
    }
    return result;
}

// sub_1001C6F0
char *AnalExecutor::setError(int32_t flag, char *msg, char *msg1033)
{
    char *result = NULL;

    if (flag)
    {
        if (gLanguage != 1033)
        {
            msg1033 = msg;
        }
        result = this->setError(msg1033);
    }
    return result;
}

// sub_1001C740
char *AnalExecutor::setError(char *Source)
{
    this->analInfo.code = ANAL_ERROR;
    strncpy((char *)this->analInfo.errMsg, Source, 0x3FFu);
    return this->analInfo.errMsg;
}

// sub_1001C640
int32_t AnalExecutor::calcAndSetMetric(int32_t head, int32_t tail, int32_t *pCount)
{
    int32_t remain;

    if (head < 0)
    {
        head = 0;
    }
    if (tail > this->pResInfWrapper->getNum() - 1)
    {
        tail = this->pResInfWrapper->getNum() - 1;
    }
    remain = this->pCaculator->calcResultNotC86(head, tail);
    if (pCount)
    {
        *pCount = remain;
    }
    return this->setConcMetric();
}

// sub_100102CE
int32_t AnalExecutor::handlePeakEnd(float time, int32_t flag)
{
    int32_t result;
    int32_t truncSize;
    int32_t k;
    int32_t vCount;
    int32_t num;
    int32_t j;
    int32_t i;
    int32_t callResult;
    int32_t ts;
    TDLLResultListItem *pRes;
    float freq;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || !num || !this->pRawBagBuffer || !(int32_t)freq)
    {
        return 0;
    }
    vCount = (int32_t)(time * 60.0 * freq);
    if (vCount >= this->pTsmpBag->getRawBagTime())
    {
        vCount = this->pTsmpBag->getRawBagTime() - 1;
    }
    for (i = 0;
         (i < num) && ((i != num - 1 && vCount > pRes[i + 1].ts && (pRes[i + 1].tr <= 0 || vCount >= pRes[i + 1].tr) && (pRes[i + 1].tr != pRes[i + 1].ts || vCount >= pRes[i + 1].te)) || ((pRes[i].te <= pRes[i].tr || vCount <= pRes[i].tr) && (pRes[i].te != pRes[i].tr || vCount <= pRes[i].ts)));
         ++i)
    {
        ;
    }
    if (i == num)
    {
        this->setError(flag, aNoFindTheGoalP0_CN, aNoFindTheGoalP0_EN);
        result = false;
    }
    else if (typeCode2Without84_116(&pRes[i], 0))
    {
        if (pRes[i].fte <= 0.0 || vCount <= pRes[i].fte)
        {
            if ((BYTE)flag)
            {
                result = true;
            }
            else
            {
                if ((pRes[i].TypeCode[0] & X7F) == C84 || (pRes[i].TypeCode[0] & X7F) == C116)
                {
                    for (j = i; j >= 0; --j)
                    {
                        if (pRes[j].TypeCode[0] == pRes[i].TypeCode[0] && typeCode1Without84_116(&pRes[j], 0) && pRes[j].tr > 0)
                        {
                            ts = pRes[j].ts;
                            break;
                        }
                    }
                }
                else
                {
                    for (j = i; j >= 0; --j)
                    {
                        if ((pRes[j].TypeCode[0] & X7F) != C84 && (pRes[j].TypeCode[0] & X7F) != C116 && typeCode1Without84_116(&pRes[j], 0) && pRes[j].tr > 0)
                        {
                            ts = pRes[j].ts;
                            break;
                        }
                    }
                }
                if (j < 0)
                {
                    j = 0;
                    ts = pRes[0].ts;
                }
                callResult = calcTailResultItem(this->pRawBagBuffer, pRes, j, i, (float)ts, (float)vCount);
                if (!callResult && (pRes[i].TypeCode[0] & X7F) == C84 && typeCode2Without84_116(&pRes[i], 0) && (i == num - 1 || typeCode1Without84_116(&pRes[i + 1], 1)))
                {
                    for (k = i; k > 0 && !isPeakType36_83_115(&pRes[k]); --k)
                    {
                        ;
                    }
                    i = k;
                    for (j = k; j > 0 && !typeCode1Without84_116(&pRes[j], 2); --j)
                    {
                        ;
                    }
                    ts = pRes[j].ts;
                    callResult = calcTailResultItem(this->pRawBagBuffer, pRes, j, k, (float)ts, (float)vCount);
                }
                if (callResult)
                {
                    this->calcAndSetMetric(j, i, NULL);
                }
                if (i < num - 1)
                {
                    truncSize = 184 * i;
                    if (vCount > pRes[i + 1].ts)
                    {
                        MLOBYTE(truncSize) = 0;
                        this->handlePeakStart(time, truncSize);
                    }
                }
                result = callResult;
            }
        }
        else
        {
            this->setError(flag, aThePeakStartCa0_CN, aThePeakStartCa0_EN);
            result = 0;
        }
    }
    else
    {
        this->setError(flag, aThePeakEndMust_CN, aThePeakEndMust_EN);
        result = 0;
    }
    return result;
}

// sub_10010953
int32_t AnalExecutor::handlePeakMoveSplit(float time, int32_t flag)
{
    int32_t result;
    int32_t breakP;
    int32_t vCount;
    int32_t num;
    int32_t j;
    double sumAreae;
    double sumAreas;
    int32_t k;
    int32_t nTe;
    int32_t nTs;
    int32_t i;
    TDLLResultListItem *pRes;
    float freq;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || !num || !this->pRawBagBuffer || !(int32_t)freq)
    {
        return false;
    }
    if (num > 1)
    {
        vCount = (int32_t)(time * 60.0 * freq);
        for (i = 0; i < num; ++i)
        {
            if (pRes[i].TypeCode[2] == C86 && vCount > pRes[i].tr && vCount <= pRes[i].te)
            {
                if (isPeakType36_83(&pRes[i]))
                {
                    breakP = typeCode84BreakAt(pRes, num, i);
                    if (breakP > 0 && (pRes[breakP - 1].TypeCode[0] & X7F) == C84 && vCount < pRes[breakP - 1].te)
                    {
                        this->setError(flag, aCanTMoveTheSpl_CN, aCanTMoveTheSpl_EN);
                        return false;
                    }
                }
                else if ((pRes[i].TypeCode[0] & X7F) == C116 || (pRes[i + 1].TypeCode[0] & X7F) != C116)
                {
                    breakP = i + 1;
                }
                else
                {
                    breakP = searchSucceedTypeCodeIdx(&pRes[i + 1], i + 1, num - 1);
                }
                if (flag)
                {
                    return false;
                }
                nTe = pRes[i].te;
                sumAreae = 0.0;
                while (nTe > vCount)
                {
                    sumAreae = (double)*(int32_t *)(this->pRawBagBuffer + 4 * nTe--) + sumAreae;
                }
                pRes[i].te = vCount;
                pRes[i].he = (float)*(int32_t *)(this->pRawBagBuffer + 4 * vCount);
                pRes[i].Areae = pRes[i].Areae - sumAreae;
                if (breakP >= 0)
                {
                    pRes[breakP].ts = pRes[i].te;
                    pRes[breakP].hs = pRes[i].he;
                    pRes[breakP].Areas = pRes[i].Areae;
                }
                break;
            }
            if (pRes[i].TypeCode[1] == C86 && vCount >= pRes[i].ts && vCount < pRes[i].tr)
            {
                if (isPeakType36_115(&pRes[i]))
                {
                    breakP = typeCode116BreakAt(pRes, i);
                    if (breakP < num - 1 && (pRes[breakP + 1].TypeCode[0] & X7F) == C116 && vCount > pRes[breakP + 1].ts)
                    {
                        this->setError(flag, aCanTMoveTheSpl0_CN, aCanTMoveTheSpl0_EN);
                        return false;
                    }
                }
                else if ((pRes[i].TypeCode[0] & X7F) == C84 || (pRes[i - 1].TypeCode[0] & X7F) != C84)
                {
                    breakP = i - 1;
                }
                else
                {
                    breakP = searchSucceedTypeCodeIdx(&pRes[i - 1], i - 1, num - 1);
                }
                if (flag)
                {
                    return true;
                }
                nTs = pRes[i].ts;
                sumAreas = 0.0;
                while (nTs < vCount)
                {
                    sumAreas = this->pRawBagBuffer[nTs++] + sumAreas;
                }
                pRes[i].ts = vCount;
                pRes[i].hs = (float)this->pRawBagBuffer[vCount];
                pRes[i].Areas = sumAreas + pRes[i].Areas;
                if (breakP >= 0)
                {
                    pRes[breakP].te = pRes[i].ts;
                    pRes[breakP].he = pRes[i].hs;
                    pRes[breakP].Areae = pRes[i].Areas;
                }
                break;
            }
        }
        if (i == num)
        {
            this->setError(flag, aNotLocateTheGo_CN, aNotLocateTheGo_EN);
            result = false;
        }
        else
        {
            for (j = i; j >= 0 && !typeCode1Without84_116(&pRes[j], 3) && j; --j)
            {
                ;
            }
            if (j < 0)
            {
                j = 0;
            }
            for (k = i; k < num && !typeCode2Without84_116(&pRes[k], 3) && k != num - 1; ++k)
            {
                ;
            }
            if (k >= num)
            {
                k = num - 1;
            }
            result = this->calcAndSetMetric(j, k, NULL);
        }
    }
    else
    {
        this->setError(flag, aThereIsNoneOfO_CN, aThereIsNoneOfO_EN);
        result = false;
    }
    return result;
}

// sub_100111A7
int32_t AnalExecutor::handlePeakAddSplit(float ftime, int32_t flag)
{
    float slope;
    int32_t time;
    double slopeThreshold;
    int32_t *pNum;
    int32_t isRight = 0;
    int32_t checkResult;
    int32_t i;
    double areae;
    TDLLResultListItem Src;
    int32_t num;
    int32_t tailVal;
    double areas;
    int32_t left;
    int32_t head;
    int32_t tail;
    int32_t mode;
    double arear;
    int32_t idx;
    int32_t headVal;
    int32_t idxVal;
    char typeCode;
    int32_t right;
    int32_t should78;
    int32_t count;
    int32_t index;
    int32_t endPoint;
    double timeEnd;
    int32_t peakFlag;
    TDLLResultListItem *pRes;
    float freq;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || !num || !this->pRawBagBuffer || !(int32_t)freq)
    {
        return false;
    }
    count = (int32_t)(ftime * 60.0 * freq);
    //    endPoint = 0;
    for (i = 0; i < num; ++i)
    {
        if ((pRes[i].TypeCode[0] & X7F) == C83 && i < num - 1)
        {
            endPoint = pRes[i + 1].ts;
        }
        else
        {
            endPoint = pRes[i].te;
        }
        if (count > pRes[i].ts && count < pRes[i].tr)
        {
            isRight = false;
            break;
        }
        if (count > pRes[i].tr && count < endPoint)
        {
            isRight = true;
            break;
        }
    }
    if (i == num)
    {
        this->setError(flag, aNotLocateTheGo0_CN, aNotLocateTheGo0_EN);
        return 0;
    }
    if ((pRes[i].TypeCode[0] & X7F) == C78)
    {
        this->setError(flag, aCanTAddASplitI_CN, aCanTAddASplitI_EN);
        return 0;
    }
    left = i;
    right = i;
    peakFlag = isPeakType36_83_115(&pRes[i]) ? 3 : 0;
    while (left > 0 && !typeCode1Without84_116(&pRes[left], peakFlag))
    {
        --left;
    }
    while (right < num && !typeCode2Without84_116(&pRes[right], peakFlag) && right < num - 1)
    {
        ++right;
    }
    int32_t timeStart = (int32_t)pRes[left].ts;
    timeEnd = (double)pRes[right].te;
    if (timeEnd <= timeStart)
    {
        slopeThreshold = 0.0;
    }
    else
    {
        slopeThreshold = (pRes[right].he - pRes[left].hs) / (timeEnd - timeStart);
    }
    head = 0;
    tail = 0;
    idx = 0;
    should78 = false;
    if (isRight)
    {
        if (isRight == 1)
        {
            if (isPeakType36_83(&pRes[i]) && i < num - 1)
            {
                tail = pRes[i + 1].ts;
            }
            else
            {
                tail = pRes[i].te;
            }
            head = count;
        }
    }
    else
    {
        if (isPeakType36_115(&pRes[i]) && i > 0)
        {
            head = pRes[i - 1].te;
        }
        else
        {
            head = pRes[i].ts;
        }
        tail = count;
    }
    mode = (isRight != 0) + 1;
    time = (int32_t)pRes[i].tr;
    slope = this->pEventManager->getNormalizeSlope();
    checkResult = searchIndexMaxOrMin(mode, this->pRawBagBuffer, slope, time, &head, &tail, &idx, slopeThreshold);
    if (checkResult)
    {
        if (checkResult == 1)
        {
            should78 = false;
        }
        else if (checkResult == 2)
        {
            should78 = true;
        }
    }
    else
    {
        if (mode == 1)
        {
            if (isPeakType36_115(&pRes[i]))
            {
                this->setError(flag, aCanTAddASplitO_CN, aCanTAddASplitO_EN);
                return 0;
            }
            checkResult = 3;
            head = pRes[i].ts;
            tail = count;
            idx = count;
        }
        else
        {
            if (isPeakType36_83(&pRes[i]))
            {
                this->setError(flag, aCanTAddASplitO0_CN, aCanTAddASplitO0_EN);
                return 0;
            }
            checkResult = 4;
            tail = pRes[i].te;
            head = count;
            idx = count;
        }
        should78 = false;
    }
    if (flag)
    {
        return true;
    }
    headVal = this->pRawBagBuffer[head];
    tailVal = this->pRawBagBuffer[tail];
    idxVal = this->pRawBagBuffer[idx];
    areas = 0.0;
    areae = 0.0;
    arear = 0.0;
    //    index = 0;
    if (isRight)
    {
        if (isRight == 1)
        {
            if (isPeakType36_83(&pRes[i]) && i < num - 1)
            {
                areae = pRes[i + 1].Areas;
            }
            else
            {
                areae = pRes[i].Areae;
            }
            index = tail;
            arear = areae;
            while (index > idx)
            {
                arear = arear - (double)this->pRawBagBuffer[index--];
            }
            index = idx;
            areas = arear;
            while (index > head)
            {
                areas = areas - (double)this->pRawBagBuffer[index--];
            }
        }
    }
    else
    {
        if (isPeakType36_115(&pRes[i]) && i > 0)
        {
            areas = pRes[i - 1].Areae;
        }
        else
        {
            areas = pRes[i].Areas;
        }
        index = head + 1;
        arear = areas;
        while (index <= idx)
        {
            arear = (double)this->pRawBagBuffer[index++] + arear;
        }
        index = idx + 1;
        areae = arear;
        while (index <= tail)
        {
            areae = (double)this->pRawBagBuffer[index++] + areae;
        }
    }
    memset(&Src, 0, sizeof(Src));
    memset((char *)&Src.TypeCode, C32, 3u);
    Src.ts = head;
    Src.te = tail;
    Src.tr = (float)idx;
    Src.hsv = (float)headVal;
    Src.hs = Src.hsv;
    Src.hev = (float)tailVal;
    Src.he = Src.hev;
    Src.hr = (float)idxVal;
    Src.Areas = areas;
    Src.Areae = areae;
    Src.Arear = arear;
    typeCode = C32;
    if (should78)
    {
        typeCode = C78;
    }
    else if (isTypeCode84_116(&pRes[i]))
    {
        typeCode = C84;
    }
    else if (isPeakType36_83_115(&pRes[i]))
    {
        if (isRight == 1)
        {
            if (i < num - 1 && isTypeCode84(&pRes[i + 1]))
            {
                if (isTypeCode36(&pRes[i]))
                {
                    setTypeCode0(&pRes[i], C115);
                }
                else
                {
                    setTypeCode0(&pRes[i], C32);
                }
                typeCode = C83;
                Src.te = pRes[i].te;
                Src.he = pRes[i].he;
                Src.hev = pRes[i].hev;
                Src.Areae = pRes[i].Areae;
                Src.ARight = Src.Areae - Src.Arear;
            }
        }
        else if (!isRight && i > 0 && isTypeCode116(&pRes[i - 1]))
        {
            if (isTypeCode36(&pRes[i]))
            {
                setTypeCode0(&pRes[i], C83);
            }
            else
            {
                setTypeCode0(&pRes[i], C32);
            }
            typeCode = C115;
            Src.ts = pRes[i].ts;
            Src.hs = pRes[i].hs;
            Src.Areas = pRes[i].Areas;
            Src.ALeft = Src.Arear - Src.Areas;
        }
    }
    Src.TypeCode[0] = typeCode;
    if (isRight)
    {
        if (isRight == 1)
        {
            Src.TypeCode[2] = pRes[i].TypeCode[2];
            if (checkResult == 4)
            {
                Src.TypeCode[0] = C66;
                Src.TypeCode[1] = C88;
                pRes[i].TypeCode[2] = C88;
            }
            else
            {
                Src.TypeCode[1] = C86;
                pRes[i].TypeCode[2] = C86;
            }
            pRes[i].te = Src.ts;
            pRes[i].he = Src.hs;
            pRes[i].Areae = Src.Areas;
        }
    }
    else
    {
        Src.TypeCode[1] = pRes[i].TypeCode[1];
        if (checkResult == 3)
        {
            Src.TypeCode[0] = C70;
            Src.TypeCode[2] = C88;
            pRes[i].TypeCode[1] = C88;
        }
        else
        {
            Src.TypeCode[2] = C86;
            pRes[i].TypeCode[1] = C86;
        }
        pRes[i].ts = Src.te;
        pRes[i].hs = Src.he;
        pRes[i].hsv = Src.hev;
        pRes[i].Areas = Src.Areae;
    }
    Src.TypeCode[0] |= 0x80u;
    pNum = this->pResInfWrapper->getNumPointer();
    if (isRight)
    {
        if (isRight == 1)
        {
            if (i < num - 1)
            {
                memmove((void *)&pRes[i + 2], (const void *)&pRes[i + 1], 184 * (num - i - 1));
            }
            memcpy((void *)&pRes[i + 1], &Src, 0xB8u);
        }
    }
    else
    {
        memmove((void *)&pRes[i + 1], (const void *)(&pRes[i]), 184 * (num - i));
        memcpy((void *)(&pRes[i]), &Src, 0xB8u);
    }
    ++*pNum;
    ++num;
    left = i;
    right = i;
    while (left > 0 && !typeCode1Without84_116(&pRes[left], 3))
    {
        --left;
    }
    while (right < num - 1 && !typeCode1Without84_116(&pRes[right + 1], 3))
    {
        ++right;
    }
    this->calcAndSetMetric(left, right, NULL);
    return true;
}

// sub_1001227D
int32_t AnalExecutor::handlePeakDelSplit(float time, int32_t flag)
{
    int32_t result;
    int32_t *pNum;
    int32_t i;
    int32_t num;
    int32_t lastIdx;
    int32_t left;
    int32_t right;
    int32_t count;
    TDLLResultListItem *pRes;
    float freq;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || num < 1 || !this->pRawBagBuffer || !(int32_t)freq)
    {
        return 0;
    }
    count = (int32_t)(time * 60.0 * freq);
    for (i = 0; i < num - 1; ++i)
    {
        if (count > pRes[i].tr && count < pRes[i + 1].tr)
        {
            if (pRes[i + 1].TypeCode[2] == C88)
            {
                if ((int32_t)(pRes[i + 1].tr - count) < count - pRes[i].te)
                {
                    ++i;
                }
            }
            else if (pRes[i].TypeCode[1] == C88 && (int32_t)(count - pRes[i].tr) < pRes[i + 1].ts - count)
            {
                --i;
            }
            break;
        }
    }
    if (i == num - 1)
    {
        this->setError(flag, aNotLocateTheGo1_CN, aNotLocateTheGo1_EN);
        result = 0;
    }
    else if (typeCode2Without84_116(&pRes[i], 0))
    {
        this->setError(flag, aNotLocateTheGo2_CN, aNotLocateTheGo2_EN);
        result = 0;
    }
    else if (flag)
    {
        result = 1;
    }
    else
    {
        pRes[i].te = pRes[i + 1].te;
        pRes[i].he = pRes[i + 1].he;
        pRes[i].Areae = pRes[i + 1].Areae;
        pRes[i].TypeCode[2] = pRes[i + 1].TypeCode[2];
        if (pRes[i].hr < (int32_t)pRes[i + 1].hr)
        {
            pRes[i].tr = pRes[i + 1].tr;
            pRes[i].hr = pRes[i + 1].hr;
            pRes[i].Arear = pRes[i + 1].Arear;
            pRes[i].TypeCode[0] = pRes[i + 1].TypeCode[0];
        }
        if (i + 1 < num - 1)
        {
            memcpy((void *)&pRes[i + 1], (const void *)&pRes[i + 2], 184 * (num - i - 2));
        }
        memset((void *)&pRes[num - 1], 0, 0xB8u);
        pNum = this->pResInfWrapper->getNumPointer();
        --*pNum;
        lastIdx = num - 1;
        left = i;
        right = i;
        while (left > 0 && !typeCode1Without84_116(&pRes[left], 3))
        {
            --left;
        }
        while (right < lastIdx - 1 && !typeCode1Without84_116(&pRes[right + 1], 3))
        {
            ++right;
        }
        this->calcAndSetMetric(left, right, NULL);
        result = 1;
    }
    return result;
}

// sub_10012739
int32_t AnalExecutor::handlePeakAddOnlyOne(float timeBegin, float timeEnd, int32_t flag)
{
    int32_t result;
    float slope;
    int32_t searchResult;
    char typeCode;
    int32_t idx;
    char k;
    char j;
    int32_t i;
    int32_t *pNum;
    double areae;
    TDLLResultListItem Src;
    int32_t hev;
    double areas;
    int32_t l;
    double arear;
    int32_t searchedIdx;
    int32_t hsv;
    int32_t hr;
    int32_t should78;

    int32_t head;
    int32_t tail;
    int32_t vCountBegin;
    int32_t vCountEnd;
    TDLLResultListItem *pRes;
    float freq;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    int32_t num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || !this->pRawBagBuffer || !(int32_t)freq)
    {
        return false;
    }
    if (timeBegin == timeEnd)
    {
        return false;
    }
    vCountBegin = 0;
    vCountEnd = 0;
    if (timeBegin >= (double)timeEnd)
    {
        vCountBegin = (int32_t)(timeEnd * 60.0 * freq);
        vCountEnd = (int32_t)(timeBegin * 60.0 * freq);
    }
    else
    {
        vCountBegin = (int32_t)(timeBegin * 60.0 * freq);
        vCountEnd = (int32_t)(timeEnd * 60.0 * freq);
    }
    int32_t rawBagLen = this->pTsmpBag->getRawBagLen();
    if (vCountBegin >= rawBagLen)
    {
        vCountBegin = rawBagLen - 1;
    }
    if (vCountEnd >= rawBagLen)
    {
        vCountEnd = rawBagLen - 1;
    }
    if (vCountBegin == vCountEnd)
    {
        this->setError(flag, aTheGoalTimeRan_CN, aTheGoalTimeRan_EN);
        result = false;
    }
    else
    {
        head = -1;
        tail = num;
        if (num > 0)
        {
            if (vCountEnd >= pRes[0].tr)
            {
                if (vCountBegin <= pRes[num - 1].tr)
                {
                    for (i = 0; i < num - 1; ++i)
                    {
                        if (vCountBegin > pRes[i].tr && vCountEnd < pRes[i + 1].tr)
                        {
                            head = i;
                            tail = i + 1;
                            break;
                        }
                    }
                }
                else
                {
                    head = num - 1;
                    tail = num;
                }
            }
            else
            {
                head = -1;
                tail = 0;
            }
        }
        if (tail - head == 1)
        {
            searchedIdx = 0;
            should78 = 0;
            slope = this->pEventManager->getNormalizeSlope();
            searchResult =
                searchIndexMaxOrMin(0, this->pRawBagBuffer, slope, 0, &vCountBegin, &vCountEnd, &searchedIdx, 0.0);
            if (searchResult)
            {
                if (searchResult == 1)
                {
                    should78 = 0;
                }
                else if (searchResult == 2)
                {
                    should78 = 1;
                }
                if (flag)
                {
                    result = 1;
                }
                else
                {
                    vCountBegin = (int32_t)searchSteepPoint(this->pRawBagBuffer, (float)vCountBegin,
                                                            (float)vCountEnd, (float)searchedIdx, should78);
                    vCountEnd = (int32_t)searchSteepPointReverse(
                        this->pRawBagBuffer, (float)vCountBegin, (float)vCountEnd, (float)searchedIdx, should78);
                    hsv = this->pRawBagBuffer[vCountBegin];
                    hev = this->pRawBagBuffer[vCountEnd];
                    hr = this->pRawBagBuffer[searchedIdx];
                    //                    areas = 0.0;
                    //                    arear = 0.0;
                    //                    i = 0;
                    if (head < 0)
                    {
                        if (tail >= num)
                        {
                            i = 0;
                            areas = 0.0;
                            while (i <= vCountBegin)
                            {
                                areas = (double)this->pRawBagBuffer[i++] + areas;
                            }
                            i = vCountBegin + 1;
                            arear = areas;
                            while (i <= searchedIdx)
                            {
                                arear = (double)this->pRawBagBuffer[i++] + arear;
                            }
                            i = searchedIdx + 1;
                            areae = arear;
                            while (i <= vCountEnd)
                            {
                                areae = (double)this->pRawBagBuffer[i++] + areae;
                            }
                        }
                        else
                        {
                            if (vCountEnd >= pRes[tail].ts)
                            {
                                i = (int32_t)pRes[tail].tr;
                                areae = pRes[tail].Arear;
                            }
                            else
                            {
                                i = pRes[tail].ts;
                                areae = pRes[tail].Areas;
                            }
                            while (i > vCountEnd)
                            {
                                areae = areae - this->pRawBagBuffer[i--];
                            }
                            i = vCountEnd;
                            arear = areae;
                            while (i > searchedIdx)
                            {
                                arear = arear - this->pRawBagBuffer[i--];
                            }
                            i = searchedIdx;
                            areas = arear;
                            while (i > vCountBegin)
                            {
                                areas = areas - this->pRawBagBuffer[i--];
                            }
                        }
                    }
                    else
                    {
                        if (vCountBegin <= pRes[head].te)
                        {
                            i = (int32_t)pRes[head].tr + 1;
                            areas = pRes[head].Arear;
                        }
                        else
                        {
                            i = pRes[head].te + 1;
                            areas = pRes[head].Areae;
                        }
                        while (i <= vCountBegin)
                        {
                            areas = this->pRawBagBuffer[i++] + areas;
                        }
                        i = vCountBegin + 1;
                        arear = areas;
                        while (i <= searchedIdx)
                        {
                            arear = this->pRawBagBuffer[i++] + arear;
                        }
                        i = searchedIdx + 1;
                        areae = arear;
                        while (i <= vCountEnd)
                        {
                            areae = this->pRawBagBuffer[i++] + areae;
                        }
                    }
                    memset((void *)&Src, 0, sizeof(TDLLResultListItem));
                    Src.ts = vCountBegin;
                    Src.te = vCountEnd;
                    Src.tr = (float)searchedIdx;
                    Src.hsv = (float)hsv;
                    Src.hs = Src.hsv;
                    Src.hev = (float)hev;
                    Src.he = (float)Src.hev;
                    Src.hr = (float)hr;
                    Src.Areas = areas;
                    Src.Areae = areae;
                    Src.Arear = arear;
                    strncpy((char *)Src.TypeCode, Source, sizeof(Source));
                    if (should78)
                    {
                        Src.TypeCode[0] = C78;
                    }
                    else if (head >= 0 && appendTypeCode0ST(&pRes[head]))
                    {
                        idx = head;
                        for (j = pRes[head].TypeCode[0] & X7F; j != C83; j = pRes[idx].TypeCode[0] & X7F)
                        {
                            if (j == C36)
                            {
                                break;
                            }
                            if (--idx < 0)
                            {
                                break;
                            }
                        }
                        if (idx >= 0 && vCountEnd <= pRes[idx].te)
                        {
                            Src.TypeCode[0] = C84;
                        }
                    }
                    else if (tail < num && appendTypeCode0st(&pRes[tail]))
                    {
                        idx = tail;
                        for (k = pRes[tail].TypeCode[0] & X7F; k != C115; k = pRes[idx].TypeCode[0] & X7F)
                        {
                            if (k == C36)
                            {
                                break;
                            }
                            if (++idx >= num)
                            {
                                break;
                            }
                        }
                        if (idx >= 0 && vCountBegin >= pRes[idx].ts)
                        {
                            Src.TypeCode[0] = C116;
                        }
                    }
                    else if (head < 0 || (double)searchedIdx < pRes[head].tr || searchedIdx > pRes[head].te)
                    {
                        if (tail >= num || searchedIdx < pRes[tail].ts || (double)searchedIdx > pRes[tail].tr)
                        {
                            Src.TypeCode[0] = C32;
                        }
                        else
                        {
                            typeCode = pRes[tail].TypeCode[0] & X7F;
                            if (typeCode == C83)
                            {
                                setTypeCode0(&pRes[tail], C36);
                            }
                            else if (typeCode != C115 && typeCode != C36)
                            {
                                setTypeCode0(&pRes[tail], C115);
                            }
                            setTypeCode0(&Src, C116);
                        }
                    }
                    else
                    {
                        typeCode = pRes[head].TypeCode[0] & X7F;
                        if (typeCode == C115)
                        {
                            setTypeCode0(&pRes[head], C36);
                        }
                        else if (typeCode != C83 && typeCode != C36)
                        {
                            setTypeCode0(&pRes[head], C83);
                        }
                        setTypeCode0(&Src, C84);
                    }
                    Src.TypeCode[0] |= 0x80u;
                    //                    l = -1;
                    //                    i = -1;
                    if ((Src.TypeCode[0] & X7F) == C84)
                    {
                        for (l = head; l > 0 && !typeCode1Without84_116(&pRes[l], 3); --l)
                        {
                            ;
                        }
                        i = head + 1;
                        if (head + 1 < num - 1)
                        {
                            while (i < num - 1 && !typeCode1Without84_116(&pRes[i + 1], 3))
                            {
                                ++i;
                            }
                        }
                        else
                        {
                            i = num - 1;
                        }
                    }
                    else if ((Src.TypeCode[0] & X7F) == C116)
                    {
                        for (l = tail; l > 0 && !typeCode1Without84_116(&pRes[l], 3); --l)
                        {
                            ;
                        }
                        i = tail + 1;
                        if (tail + 1 < num - 1)
                        {
                            while (i < num - 1 && !typeCode1Without84_116(&pRes[i + 1], 3))
                            {
                                ++i;
                            }
                        }
                        else
                        {
                            i = num - 1;
                        }
                    }
                    else
                    {
                        i = tail;
                        l = tail;
                    }
                    pNum = this->pResInfWrapper->getNumPointer();
                    if (tail < *pNum)
                    {
                        memmove((void *)&pRes[tail + 1], (const void *)&pRes[tail], 184 * (num - tail));
                    }
                    ++*pNum;
                    ++num;
                    memcpy((void *)&pRes[tail], &Src, 0xB8u);
                    this->calcAndSetMetric(l, i + 1, NULL);
                    result = true;
                }
            }
            else
            {
                this->setError(flag, aCanTRecogniseA_CN, aCanTRecogniseA_EN);
                result = false;
            }
        }
        else
        {
            this->setError(flag, aCanTLocateTheG_CN, aCanTLocateTheG_EN);
            result = false;
        }
    }
    return result;
}

// sub_10013806
int32_t AnalExecutor::handlePeakDelete(float timeStart, float timeEnd, int32_t flag)
{
    int32_t result;
    int32_t *numAddr;
    int32_t num;
    int32_t head;
    int32_t i;
    int32_t tail;
    int32_t j;
    TDLLResultListItem *pRes;
    float freq;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || !num || !this->pRawBagBuffer || !freq)
    {
        return false;
    }
    if (checkTrPeak(&head, &tail, timeStart, timeEnd, pRes, num, freq))
    {
        if (flag)
        {
            result = true;
        }
        else
        {
            i = head;
            //            j = -1;
            while (i <= tail)
            {
                if (isTypeCode84_116(&pRes[i]))
                {
                    j = searchSucceedTypeCodeIdx(&pRes[i], i, num - 1);
                    if (j >= 0)
                    {
                        pRes[j].Area = pRes[j].Area + pRes[i].Area;
                    }
                }
                ++i;
            }
            if (tail < num - 1)
            {
                memcpy((void *)(&pRes[head]), (const void *)(&pRes[tail + 1]),
                       sizeof(TDLLResultListItem) * (num - tail - 1));
            }
            memset((void *)(&pRes[num - tail + head - 1]), 0,
                   sizeof(TDLLResultListItem) * (tail - head + 1));
            numAddr = this->pResInfWrapper->getNumPointer();
            *numAddr -= tail - head + 1;
            if (head > 0 && isPeakType36_83(&pRes[head - 1]) && !isTypeCode84(&pRes[head]))
            {
                if (isTypeCode36(&pRes[head - 1]))
                {
                    setTypeCode0(&pRes[head - 1], C115);
                }
                else
                {
                    setTypeCode0(&pRes[head - 1], C32);
                }
            }
            if (head >= 0 && isPeakType36_115(&pRes[head]) && (!head || !isTypeCode116(&pRes[head - 1])))
            {
                if (isTypeCode36(&pRes[head]))
                {
                    setTypeCode0(&pRes[head], C83);
                }
                else
                {
                    setTypeCode0(&pRes[head], C32);
                }
            }
            result = true;
        }
    }
    else
    {
        this->setError(flag, aThereIsNtAnyPe_CN, aThereIsNtAnyPe_EN);
        result = false;
    }
    return result;
}

// sub_10013D9D
int32_t AnalExecutor::handleBaseValley(float timeStart, float timeEnd, int32_t flag)
{
    int32_t result;
    int32_t m;
    int32_t l;
    int32_t k;
    int32_t j;
    int32_t i;
    int32_t num;
    int32_t head;
    int32_t n;
    int32_t remain;
    int32_t tail;
    int32_t hasCode83;
    TDLLResultListItem *pRes;
    float freq;

    remain = 0;
    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || !num || !this->pRawBagBuffer || !freq)
    {
        return false;
    }
    if (checkTrPeak(&head, &tail, timeStart, timeEnd, pRes, num, freq))
    {
        if (flag)
        {
            result = true;
        }
        else
        {
            while (head > 0 && (pRes[head].TypeCode[0] & X7F) == C84 && !typeCode1Without84_116(&pRes[head], 0))
            {
                --head;
            }
            hasCode83 = false;
            if ((pRes[head].TypeCode[0] & X7F) == C84)
            {
                for (i = head - 1; i > 0; --i)
                {
                    if ((pRes[i].TypeCode[0] & X7F) == C83)
                    {
                        pRes[i].te = pRes[head].ts;
                        pRes[i].he = pRes[head].hs;
                        pRes[i].Areae = pRes[head].Areas;
                        pRes[i].TypeCode[2] = C66;
                        if (i == head - 1)
                        {
                            pRes[i].TypeCode[0] = C32;
                        }
                        hasCode83 = true;
                        break;
                    }
                }
            }
            if ((pRes[tail].TypeCode[0] & X7F) == C84)
            {
                while ((pRes[tail].TypeCode[0] & X7F) == C84 && !typeCode2Without84_116(&pRes[tail], 0))
                {
                    ++tail;
                }
            }
            if ((pRes[tail].TypeCode[0] & X7F) == C84 || (pRes[tail].TypeCode[0] & X7F) == C83)
            {
                for (j = tail + 1; pRes[j].TypeCode[0] == C84; pRes[j++ + 1].TypeCode[0] = C32)
                {
                    ;
                }
                if (j <= num - 1 && pRes[j].TypeCode[1] == C86)
                {
                    pRes[j].TypeCode[1] = C66;
                    for (k = j; k < num && pRes[k].TypeCode[2] != C66; ++k)
                    {
                        ;
                    }
                    this->calcAndSetMetric(j, k, &remain);
                    if (remain > 0)
                    {
                        num -= remain;
                    }
                }
            }
            if (head > 0 && (pRes[head - 1].TypeCode[2] == C86 || pRes[head - 1].TypeCode[2] == C88 || hasCode83))
            {
                pRes[head - 1].TypeCode[2] = C66;
                for (l = head - 1; l >= 0 && !typeCode1Without84_116(&pRes[l], 3); --l)
                    ;
                this->calcAndSetMetric(l, head - 1, &remain);
                if (remain > 0)
                {
                    head -= remain;
                    tail -= remain;
                    num -= remain;
                }
            }
            if (tail < num - 1 && (pRes[tail + 1].TypeCode[1] == C86 || pRes[tail + 1].TypeCode[1] == C88))
            {
                pRes[tail + 1].TypeCode[1] = C66;
                for (m = tail + 1; m <= num - 1 && !typeCode2Without84_116(&pRes[m], 3); ++m)
                {
                    ;
                }
                this->calcAndSetMetric(tail + 1, m, &remain);
            }
            for (n = head; n <= tail; ++n)
            {
                if ((pRes[n].TypeCode[0] & X7F) == C83)
                {
                    pRes[n].te = pRes[n + 1].ts;
                    pRes[n].he = pRes[n + 1].hs;
                    pRes[n].Areae = pRes[n + 1].Areas;
                }
                if (appendTypeCode0STst(&pRes[n]))
                {
                    pRes[n].TypeCode[0] = C32;
                }
                if (pRes[n].TypeCode[1] != C88)
                {
                    pRes[n].TypeCode[1] = C66;
                }
                pRes[n].TypeCode[2] = C66;
            }
            if (tail >= head)
            {
                this->calcAndSetMetric(head, tail, NULL);
            }
            result = true;
        }
    }
    else
    {
        this->setError(flag, aThereIsNtAnyPe0_CN, aThereIsNtAnyPe0_EN);
        result = false;
    }
    return result;
}

// sub_10015A2D
int32_t AnalExecutor::handleBaseFrontTangent(float timeStart, float timeEnd, int32_t flag)
{
    int32_t result;
    int32_t k;
    int32_t j;
    int32_t te;
    int32_t he;
    int32_t num;
    int32_t head;
    int32_t time;
    int32_t i;
    int32_t tail;
    int32_t ii;
    int32_t l;
    float freq;
    TDLLResultListItem *pRes;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || !num || !this->pRawBagBuffer || !freq)
    {
        return false;
    }
    if (checkTrPeak(&head, &tail, timeStart, timeEnd, pRes, num, freq))
    {
        if (typeCode2Without84_116(&pRes[tail], 2))
        {
            this->setError(flag, aCanTMakeFrontT_CN, aCanTMakeFrontT_EN);
            result = false;
        }
        else
        {
            while (head < tail && appendTypeCode0STst(&pRes[head]))
            {
                ++head;
            }
            while (tail > head && appendTypeCode0STst(&pRes[tail]))
            {
                --tail;
            }
            for (i = head; i <= tail; ++i)
            {
                if (appendTypeCode0STst(&pRes[i]))
                {
                    this->setError(flag, aThereAreSomeTa0_CN, aThereAreSomeTa0_EN);
                    return false;
                }
            }
            if ((uint8_t)flag)
            {
                result = true;
            }
            else
            {
                if ((pRes[tail + 1].TypeCode[0] & X7F) == C116)
                {
                    for (j = tail + 1; j < num - 1 && !isPeakType36_115(&pRes[j]); ++j)
                    {
                        ;
                    }
                }
                else
                {
                    j = tail + 1;
                    if ((pRes[tail + 1].TypeCode[0] & X7F) == C83)
                    {
                        pRes[j].TypeCode[0] = C36;
                    }
                    else
                    {
                        pRes[j].TypeCode[0] = C115;
                    }
                }
                pRes[j].TypeCode[1] = pRes[head].TypeCode[1];
                pRes[j].ts = pRes[head].ts;
                pRes[j].hs = pRes[head].hs;
                pRes[j].Areas = pRes[head].Areas;
                //                time = 0;
                PeakCaculator *pPeakCaculator = new PeakCaculator();
                if (pPeakCaculator != NULL)
                {
                    pPeakCaculator = pPeakCaculator->init(this->pRawBagBuffer, 5, 7);
                }
                ii = tail;
                i = tail;
                te = pRes[tail].te;
                he = (int32_t)pRes[tail].he;
                while (i >= head)
                {
                    pPeakCaculator->calcBaseFrontTangentParams((int32_t)pRes[i].tr);
                    time = pPeakCaculator->findBaseFrontTangentPeakTime(te, he, pRes[i].ts);
                    if (time > 0 || i == head)
                    {
                        pRes[ii].TypeCode[2] = C66;
                        pRes[ii].TypeCode[1] = C66;
                        for (k = ii; k >= i; --k)
                        {
                            pRes[k].TypeCode[0] = C116;
                        }
                        if (time > 0)
                        {
                            for (l = pRes[i].ts; l < time; ++l)
                            {
                                pRes[i].Areas = this->pRawBagBuffer[l] + pRes[i].Areas;
                            }
                            pRes[i].ts = time;
                            pRes[i].hs = (float)this->pRawBagBuffer[time];
                        }
                        if (i > head)
                        {
                            ii = i - 1;
                            te = pRes[i - 1].te;
                            he = (int32_t)pRes[i - 1].he;
                        }
                    }
                    --i;
                }
                if (pPeakCaculator)
                {
                    pPeakCaculator->shutdown(1);
                }
                while (
                    head > 0 && (isPeakType36_83(&pRes[head - 1]) || !typeCode2Without84_116(&pRes[head - 1], 3)))
                {
                    --head;
                }
                while (
                    tail < num - 1 && (isPeakType36_115(&pRes[tail + 1]) || !typeCode1Without84_116(&pRes[tail + 1], 3)))
                {
                    ++tail;
                }
                this->calcAndSetMetric(head, tail, NULL);
                result = true;
            }
        }
    }
    else
    {
        this->setError(flag, aThereIsNtAnyPe3_CN, aThereIsNtAnyPe3_EN);
        result = false;
    }
    return result;
}

// sub_10014402
int32_t AnalExecutor::handleBaseTogether_(float timeStart, float timeEnd, int32_t flag)
{
    int32_t result;
    int32_t ii;
    int32_t jj;
    int32_t time;
    int32_t m;
    int32_t l;
    int32_t k;
    int32_t tempInt;
    int32_t j;
    int32_t idx;
    int32_t i;
    int32_t count;
    int32_t num;
    int32_t head;
    int32_t n;
    int32_t tail;
    char typeCode;
    TDLLResultListItem *pRes;
    float freq;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || !num || !this->pRawBagBuffer || !(int32_t)freq)
    {
        return false;
    }
    if (!searchPeakA(&head, &tail, timeStart, timeEnd, pRes, num, freq))
    {
        this->setError(flag, aThereIsNtAnyPe1_CN, aThereIsNtAnyPe1_EN);
        return false;
    }
    if (isTypeCode84_116(&pRes[head]) || isTypeCode84_116(&pRes[tail]))
    {
        goto LABEL_21;
    }
    if (tail - head < 1)
    {
        this->setError(flag, aThereIsOnlyOne_CN, aThereIsOnlyOne_EN);
        return false;
    }
    count = -1;
    for (i = head; i < tail; ++i)
    {
        if (isPeakType36_83_115(&pRes[i]) || typeCode2Without84_116(&pRes[i], 0) || typeCode1Without84_116(&pRes[i + 1], 0))
        {
            count = i;
            break;
        }
    }
    if (count >= 0)
    {
    LABEL_21:
        if (flag)
        {
            result = 1;
        }
        else
        {
            typeCode = pRes[head].TypeCode[0] & X7F;
            if (typeCode == C84 || typeCode == C116)
            {
                head = typeCode1Break84_116Prev(pRes, head, 0);
                idx = searchSucceedTypeCodeIdx(&pRes[head], head, num - 1);
                if (idx >= 0)
                {
                    if (typeCode == C84)
                    {
                        pRes[idx].te = pRes[head].ts;
                        pRes[idx].he = pRes[head].hs;
                        pRes[idx].Areae = pRes[head].Areas;
                        pRes[head].TypeCode[1] = C86;
                        pRes[idx].TypeCode[2] = C86;
                        if (idx + 1 == head)
                        {
                            if ((pRes[idx].TypeCode[0] & X7F) == C36)
                            {
                                setTypeCode0(&pRes[idx], C115);
                            }
                            else
                            {
                                setTypeCode0(&pRes[idx], C32);
                            }
                        }
                    }
                    else if (typeCode == C116)
                    {
                        for (j = head; j > 0 && (pRes[j - 1].TypeCode[0] & X7F) == C116; --j)
                        {
                            setTypeCode0(&pRes[j - 1], C32);
                        }
                        if (j < head && j > 0 && pRes[j - 1].TypeCode[1] != C66)
                        {
                            pRes[j - 1].TypeCode[1] = C66;
                            for (k = j - 1; k > 0 && !typeCode1Without84_116(&pRes[k], 3); --k)
                            {
                                ;
                            }
                            tempInt = 0;
                            this->calcAndSetMetric(k, j - 1, &tempInt);
                            if (tempInt > 0)
                            {
                                num -= tempInt;
                            }
                        }
                    }
                }
            }
            typeCode = pRes[tail].TypeCode[0] & X7F;
            if (typeCode == C84 || typeCode == C116)
            {
                tail = typeCode2Break84_116Next(pRes, num, tail, 0);
                idx = searchSucceedTypeCodeIdx(&pRes[tail], tail, num - 1);
                if (idx >= 0)
                {
                    if (typeCode == C116)
                    {
                        pRes[idx].ts = pRes[tail].te;
                        pRes[idx].hs = pRes[tail].he;
                        pRes[idx].Areas = pRes[tail].Areae;
                        pRes[tail].TypeCode[2] = C86;
                        pRes[idx].TypeCode[1] = C86;
                        if (idx - 1 == tail)
                        {
                            if ((pRes[tail].TypeCode[0] & X7F) == C36)
                            {
                                setTypeCode0(&pRes[idx], C83);
                            }
                            else
                            {
                                setTypeCode0(&pRes[idx], C32);
                            }
                        }
                    }
                    else if (typeCode == C84)
                    {
                        for (l = tail; l < num - 1 && (pRes[l + 1].TypeCode[0] & X7F) == C84; ++l)
                        {
                            setTypeCode0(&pRes[l + 1], C32);
                        }
                        if (l > tail && l < num - 1 && pRes[l + 1].TypeCode[1] != C66)
                        {
                            pRes[l + 1].TypeCode[1] = C66;
                            for (m = l + 1; m < num - 1 && !typeCode2Without84_116(&pRes[m], 3); ++m)
                            {
                                ;
                            }
                            tempInt = 0;
                            this->calcAndSetMetric(l + 1, m, &tempInt);
                            if (tempInt > 0)
                            {
                                num -= tempInt;
                            }
                        }
                    }
                }
            }
            for (n = head; n <= tail; ++n)
            {
                if (n >= tail)
                {
                    if (n < num - 1 && (pRes[n].TypeCode[0] & X7F) == C84 && pRes[n + 1].TypeCode[1] == 86)
                    {
                        pRes[n].te = pRes[n + 1].ts;
                        pRes[n].he = pRes[n + 1].hs;
                        pRes[n].Areae = pRes[n + 1].Areas;
                        pRes[n].TypeCode[2] = pRes[n + 1].TypeCode[1];
                    }
                }
                else
                {
                    if ((pRes[n].TypeCode[0] & X7F) == C83 || ((pRes[n].TypeCode[0] & X7F) == C84 && pRes[n].TypeCode[2] == C66))
                    {
                        pRes[n].te = pRes[n + 1].ts;
                        pRes[n].he = pRes[n + 1].hs;
                        pRes[n].Areae = pRes[n + 1].Areas;
                        pRes[n + 1].TypeCode[1] = C86;
                        pRes[n].TypeCode[2] = C86;
                    }
                    if (typeCode2Without84_116(&pRes[n], 0))
                    {
                        pRes[n + 1].TypeCode[1] = C86;
                        pRes[n].TypeCode[2] = C86;
                        if ((pRes[n + 1].ts + pRes[n].te) % 2)
                        {
                            time = pRes[n].te;
                            pRes[n + 1].ts = (pRes[n + 1].ts + time) / 2;
                            pRes[n].te = pRes[n + 1].ts;
                            pRes[n + 1].hs = (float)this->pRawBagBuffer[pRes[n].te];
                            pRes[n].he = pRes[n + 1].hs;
                            if (pRes[n].te <= time)
                            {
                                for (ii = time - 1; ii >= pRes[n].te; --ii)
                                {
                                    pRes[n].Areae = pRes[n].Areae - (double)this->pRawBagBuffer[ii];
                                }
                            }
                            else
                            {
                                for (jj = time + 1; jj <= pRes[n].te; ++jj)
                                {
                                    pRes[n].Areae = (double)this->pRawBagBuffer[jj] + pRes[n].Areae;
                                }
                            }
                            pRes[n + 1].Areas = pRes[n].Areae;
                        }
                        else
                        {
                            pRes[n + 1].ts = (pRes[n + 1].ts + pRes[n].te) / 2;
                            pRes[n].te = pRes[n + 1].ts;
                            pRes[n + 1].hs = (float)*(int32_t *)(this->pRawBagBuffer + 4 * pRes[n].te);
                            pRes[n].he = pRes[n + 1].hs;
                            pRes[n + 1].Areas = (pRes[n].Areae + pRes[n + 1].Areas) / 2.0;
                            pRes[n].Areae = pRes[n + 1].Areas;
                        }
                    }
                }
                if (appendTypeCode0STst(&pRes[n]))
                {
                    pRes[n].TypeCode[0] = C32;
                }
            }
            while (head > 0 && !typeCode1Without84_116(&pRes[head], 3))
            {
                --head;
            }
            while (tail < num - 1 && !typeCode2Without84_116(&pRes[tail], 3))
            {
                ++tail;
            }
            this->calcAndSetMetric(head, tail, NULL);
            result = true;
        }
    }
    else
    {
        this->setError(flag, aThePeaksInside_CN, aThePeaksInside_EN);
        result = false;
    }
    return result;
}

// sub_100153D1
int32_t AnalExecutor::handleBaseTailTangent(float timeStart, float timeEnd, int32_t flag)
{
    int32_t result;
    int32_t l;
    int32_t k;
    int32_t time;
    int32_t num;
    int32_t ts;
    int32_t j;
    int32_t jj;
    float hs;
    int32_t i;
    int32_t tail;
    int32_t m;
    int32_t head;
    TDLLResultListItem *pRes;
    float freq;

    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    num = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pRes || !num || !this->pRawBagBuffer || !freq)
    {
        return false;
    }
    if (checkTrPeak(&head, &tail, timeStart, timeEnd, pRes, num, freq))
    {
        if (typeCode1Without84_116(&pRes[head], 1))
        {
            this->setError(flag, aCanTMakeTangen_EN, aCanTMakeTangen_CN);
            result = false;
        }
        else
        {
            for (i = head; i <= tail; ++i)
            {
                if (isPeakType36_83_115(&pRes[i]))
                {
                    this->handleBaseTogether_(timeStart, timeEnd, flag);
                    if (flag)
                    {
                        return 1;
                    }
                    break;
                }
            }
            while (head < tail && appendTypeCode0STst(&pRes[head]))
            {
                ++head;
            }
            while (tail > head && appendTypeCode0STst(&pRes[tail]))
            {
                --tail;
            }
            for (j = head; j <= tail; ++j)
            {
                if (appendTypeCode0STst(&pRes[j]))
                {
                    this->setError(flag, aThereAreSomeTa_CN, aThereAreSomeTa_EN);
                    return false;
                }
            }
            if ((uint8_t)flag)
            {
                result = true;
            }
            else
            {
                if ((pRes[head - 1].TypeCode[0] & X7F) == C84)
                {
                    for (k = head - 1; k > 0 && !isPeakType36_83(&pRes[k]); --k)
                    {
                        ;
                    }
                }
                else
                {
                    k = head - 1;
                    if ((pRes[head - 1].TypeCode[0] & X7F) == C115)
                    {
                        pRes[k].TypeCode[0] = C36;
                    }
                    else
                    {
                        pRes[k].TypeCode[0] = C83;
                    }
                }
                pRes[k].TypeCode[2] = pRes[tail].TypeCode[2];
                pRes[k].te = pRes[tail].te;
                pRes[k].he = pRes[tail].he;
                pRes[k].Areae = pRes[tail].Areae;
                //                time = 0;
                PeakCaculator *pPeakCaculator = new PeakCaculator();
                if (pPeakCaculator != NULL)
                {
                    pPeakCaculator = pPeakCaculator->init(this->pRawBagBuffer, 5, 7);
                }
                jj = head;
                j = head;
                ts = pRes[head].ts;
                hs = pRes[head].hs;
                while (j <= tail)
                {
                    pPeakCaculator->calcBaseTailTangentParams((int32_t)pRes[j].tr);
                    time = pPeakCaculator->findBaseTailTangentPeakTime(ts, hs, pRes[j].te);
                    if (time > 0 || j == tail)
                    {
                        pRes[jj].TypeCode[1] = C66;
                        pRes[j].TypeCode[2] = C66;
                        for (l = jj; l <= j; ++l)
                        {
                            pRes[l].TypeCode[0] = C84;
                        }
                        if (time > 0)
                        {
                            for (m = pRes[j].te; m > time; --m)
                            {
                                pRes[j].Areae = pRes[j].Areae - this->pRawBagBuffer[m];
                            }
                            pRes[j].te = time;
                            pRes[j].he = (float)this->pRawBagBuffer[time];
                        }
                        if (j < tail)
                        {
                            jj = j + 1;
                            ts = pRes[j + 1].ts;
                            hs = pRes[j + 1].hs;
                        }
                    }
                    ++j;
                }
                if (pPeakCaculator)
                {
                    pPeakCaculator->shutdown(1);
                }
                while (head > 0 && !typeCode1Without84_116(&pRes[head], 3))
                {
                    --head;
                }
                while (tail < num - 1 && !typeCode1Without84_116(&pRes[tail + 1], 3))
                {
                    ++tail;
                }
                this->calcAndSetMetric(head, tail, NULL);
                result = true;
            }
        }
    }
    else
    {
        this->setError(flag, aThereIsNtAnyPe2_EN, aThereIsNtAnyPe2_CN);
        result = false;
    }
    return result;
}

// sub_1000DC5F
CODE AnalExecutor::execAnal()
{
    return this->_execAnal();
}

TDLLResultListItem *Item;
TDLLIDTListItem *pIDT;
CResInfWrapper *pRes;
AnalExecutor *pExecuter;
// sub_1000D764
CODE AnalExecutor::_execAnal()
{
    CODE result;
    int32_t count;
    int32_t len;
    char *errMsg;
    int32_t *rbagBuf;
    float slope;
    char *Source;
    int32_t evWidth;
    int32_t rawbagDrop;
    int32_t evBaseLock1;
    int32_t evBaseNegative1;
    int32_t rbResult;
    int32_t lastIdx;

    pExecuter = this;
    pRes = this->pResInfWrapper;
    if (this->pTsmpBag->rawbag == NULL)
    {
        return ANAL_PEAKEND;
    }
    // std::cout << "sizeof(TDLLTPRGListItem):" << sizeof( TDLLTPRGListItem ) << std::endl;
    // if(this->pTsmpBag->getRawBagTime()!=this->pTsmpBag->rawbag->len){
    //     *this->pTsmpBag->rawbag->time = this->pTsmpBag->rawbag->len;
    //}
    Item = (TDLLResultListItem *)this->pResInfWrapper->head;
    pIDT = (TDLLIDTListItem *)(this->pConcCaculator->pIdtInfWrapper->head);
    while (1)
    {
        if (this->rawbagOffset == 10000)
        {
            println("1.rawbagOffset=%d; getRawBagTime=%d", this->rawbagOffset, this->pTsmpBag->getRawBagTime());
        }
        if (this->rawbagOffset >= this->pTsmpBag->getRawBagTime())
        {
            if (this->pEventManager->checkStop(this->rawbagOffset))
            {
                this->stop();
                this->analInfo.code = ANAL_STOP;
                result = ANAL_STOP;
            }
            else
            {
                this->analInfo.code = ANAL_OK;
                result = ANAL_OK;
            }
            return result;
        }
        rbResult = this->calcAbsoluteDrop();
        if (this->pEventManager->checkStop(this->rawbagOffset))
        {
            this->analInfo.code = ANAL_STOP;
            return ANAL_STOP;
        }
        this->pEventManager->setEventData(this->rawbagOffset);
        lastIdx = this->setEventWrapperData(this->rawbagOffset);
        if (lastIdx > 0)
        {
            evBaseLock1 = this->isEventType1(BASE_LOCK);
            evBaseNegative1 = this->isEventType1(BASE_NEGATIVE);
            if (evBaseLock1 || evBaseNegative1)
            {
                this->stop_(lastIdx);
                if (evBaseNegative1)
                {
                    this->pEventManager->topOrBottom = true;
                }
            }
            if (this->isEventType1(BASE_TAIL_TANGENT))
            {
                this->pCaculator->setBaseTailTangant(true);
            }
            if (this->isEventType1(BASE_MERGE_UPMOST))
            {
                this->pEventManager->evPara[BASE_MERGE_UPMOST] = (float)this->pCaculator->baseIdx;
                if (this->pCaculator->getTickmarkStatus())
                {
                    ++this->pEventManager->evPara[BASE_MERGE_UPMOST];
                }
            }
        }
        if (rbResult)
        {
            if (this->rawbagOffset <= this->evCount)
            {
                if (this->rawbagOffset == this->evCount)
                {
                    this->savedRawbagDrop = this->absoluteDrop;
                }
                ++this->rawbagOffset;
                this->analInfo.code = ANAL_OK;
                return ANAL_OK;
            }
            count = this->pEventManager->eventWidth;
            if (this->pBuffer10Wrapper->accBuffer10Value(this->rawbagOffset, this->absoluteDrop, count, 1))
            {
                rbagBuf = this->pRawBagBuffer;
                count = this->pEventManager->eventWidth;
                this->pBuffer10Wrapper->deductArea(count, 1, rbagBuf);
                if (this->pEventManager->testParaCalcCounter())
                {
                    slope = this->pEventManager->getNormalizeSlope();
                    this->pBuffer10Wrapper->countBuffer10Type(slope);
                    if (this->pEventManager->isSlopeNearZero())
                    {
                        if (this->pEventManager->setIParaValues(this->pBuffer10Wrapper))
                        {
                            ++this->rawbagOffset;
                            this->togglerAutoNegative = this->pEventManager->turnOffAutoNegative();
                            evWidth = this->pEventManager->eventWidth;
                            rawbagDrop = this->savedRawbagDrop;
                            this->start();
                            if (this->evCount > 0)
                            {
                                this->rawbagOffset = this->evCount + 1;
                                this->absoluteDrop = rawbagDrop;
                            }
                            this->pEventManager->setWidth(evWidth);
                            this->pEventManager->setWidth0(evWidth);
                            this->analInfo.code = ANAL_TEST;
                            return ANAL_TEST;
                        }
                    }
                    else
                    {
                        if (!this->hasEventType(BASE_LOCK))
                        {
                            this->doNextAccordingStatus();
                        }
                        if (this->analInfo.code == ANAL_STOP || this->analInfo.code == ANAL_ERROR)
                        {
                            this->stop();
                            return this->analInfo.code;
                        }
                        if (this->pCaculator->finishFlag)
                        {
                            if (this->calcResultAndSetMetric())
                            {
                                ++this->rawbagOffset;
                                this->analInfo.code = ANAL_PEAKEND;
                                result = ANAL_PEAKEND;
                            }
                            else
                            {
                                if (gLanguage == 1033)
                                {
                                    Source = aNumberOfPeaksO1_EN;
                                }
                                else
                                {
                                    Source = (char *)&aNumberOfPeaksO1_CN;
                                }
                                len = this->pResInfWrapper->len;
                                errMsg = formatExceedError(Source, len);
                                strncpy((char *)this->analInfo.errMsg, errMsg, len);
                                this->analInfo.code = ANAL_ERROR;
                                result = ANAL_ERROR;
                            }
                            return result;
                        }
                    }
                }
            }
        }
        if (this->isEventType1(BASE_LOCK) || this->isEventType1(BASE_NEGATIVE) || this->isEventType2(BASE_LOCK) || this->isEventType2(BASE_NEGATIVE))
        {
            break;
        }
        ++this->rawbagOffset;
    }
    if (this->isEventType2(BASE_LOCK) || this->isEventType2(BASE_NEGATIVE))
    {
        this->stop_(lastIdx);
        if (this->isEventType2(BASE_NEGATIVE))
        {
            this->pEventManager->topOrBottom = false;
        }
    }
    ++this->rawbagOffset;
    this->analInfo.code = ANAL_PEAKEND;
    return ANAL_PEAKEND;
}

// sub_1000DD54
int32_t AnalExecutor::calcAbsoluteDrop()
{
    int32_t result;
    int32_t i;

    this->pRawBagBuffer[this->rawbagOffset] = this->pTsmpBag->getRawBagDrop(this->rawbagOffset);
    if (this->rawbagOffset > 1)
    {
        this->absoluteDrop -= this->pTsmpBag->getRawBagDrop(this->rawbagOffset - 1);
        this->absoluteDrop += this->pTsmpBag->getRawBagDrop(this->rawbagOffset);
        result = true;
    }
    else
    {
        this->absoluteDrop += this->pTsmpBag->getRawBagDrop(this->rawbagOffset);
        if (this->rawbagOffset == 1)
        {
            for (i = 0; i <= 0; ++i)
            {
                this->absoluteDrop -= this->pTsmpBag->getRawBagDrop(i);
            }
        }
        result = false;
    }
    return result;
}

// sub_1000D31F
int32_t AnalExecutor::setEventWrapperData(int32_t rbIdx)
{
    int32_t rVal_3;
    int32_t rVal_2;
    int32_t rVal_1;
    int32_t rVal0;
    int32_t rbVal;
    int32_t height2;
    int32_t height1;
    int32_t height0;
    int32_t eType;

    if (!this->pEventManager->setEventTypes(rbIdx))
    {
        return 0;
    }
    if (this->isEventType1(BASE_TOGETHER))
    {
        this->pCaculator->calcIndex1ResultEndValue();
    }
    else
    {
        this->isEventType2(BASE_TOGETHER);
    }
    if (this->hasEventType(BASE_NEGATIVE) && this->checkEmptyEvent(BASE_NEGATIVE))
    {
        eType = this->pEventManager->evType[BASE_NEGATIVE];
        if (eType == 1)
        {
            this->pEventManager->rawbagIdx = rbIdx;
            this->pEventManager->rawbagVal = this->pRawBagBuffer[rbIdx];
        }
        else if (eType == 3 && !((TDLLResultListItem *)(this->pCaculator->head))->tr && this->pBuffer10Wrapper->getBuffer10Time(2) > this->pEventManager->rawbagIdx)
        {
            rbVal = this->pEventManager->rawbagVal;
            height0 = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(0);
            height1 = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(1);
            height2 = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(2);
            if (height0 >= rbVal && height1 < height0 && height2 < height1)
            {
                this->pEventManager->evCount[BASE_NEGATIVE] = this->pBuffer10Wrapper->getBuffer10Time(0);
                this->pEventManager->evType[BASE_NEGATIVE] = 2;
                this->pEventManager->rawbagIdx = 0;
                this->pEventManager->rawbagVal = 0;
                return rbIdx;
            }
        }
    }
    if (this->hasEventType(BASE_NEGATIVE) && !this->pEventManager->x124)
    {
        return rbIdx;
    }
    eType = this->pEventManager->evType[BASE_M_NEGATIVE];
    if (eType == 1)
    {
        this->pEventManager->x124 = 1;
        this->pCaculator->setMinimumHS((int32_t *)this->pEventManager->hsMin);
    }
    else if (eType == 2)
    {
        this->pEventManager->x124 = 0;
        *this->pEventManager->hsMin = 0;
    }
    else if (this->pEventManager->x124)
    {
        if (*this->pEventManager->hsMin)
        {
            if (rbIdx >= 3)
            {
                rVal0 = this->pRawBagBuffer[rbIdx];
                rVal_1 = this->pRawBagBuffer[rbIdx - 1];
                rVal_2 = this->pRawBagBuffer[rbIdx - 2];
                rVal_3 = this->pRawBagBuffer[rbIdx - 3];
                if (this->pEventManager->topOrBottom)
                {
                    if (rVal0 > rVal_1 && rVal_1 > rVal_2 && rVal_2 > rVal_3 && rVal0 >= *this->pEventManager->hsMin)
                    {
                        this->pEventManager->evType[BASE_NEGATIVE] = 2;
                        this->pEventManager->evCount[BASE_NEGATIVE] = rbIdx;
                    }
                }
                else if (rVal0 < rVal_1 && rVal_1 < rVal_2 && rVal_2 < rVal_3 && rVal0 <= *this->pEventManager->hsMin)
                {
                    this->pEventManager->evType[BASE_NEGATIVE] = 1;
                    this->pEventManager->evCount[BASE_NEGATIVE] = -1;
                }
            }
        }
        else if (this->pCaculator->typeCodeStatus)
        {
            *this->pEventManager->hsMin = (int32_t)((TDLLResultListItem *)this->pCaculator->head)->hs;
        }
    }
    return rbIdx;
}

// sub_1001B5A0
int32_t AnalExecutor::isEventType1(EVENT event)
{
    return this->pEventManager->evType[event] == 1;
}

// sub_1001B5D0
int32_t AnalExecutor::isEventType2(EVENT event)
{
    return this->pEventManager->evType[event] == 2;
}

// sub_1001B600
int32_t AnalExecutor::hasEventType(EVENT event)
{
    return this->pEventManager->evType[event] != 0;
}

// sub_1001B630
int32_t AnalExecutor::checkEmptyEvent(EVENT event)
{
    int32_t evCount;

    evCount = this->pEventManager->evCount[event];
    return !evCount || evCount == -1;
}

// sub_1000DE42
void AnalExecutor::doNextAccordingStatus()
{
    switch (this->analInfo.status)
    {
    case 0:
        this->doNextStatus0();
        break;
    case 1:
        this->doNextStatus1();
        break;
    case 2:
        this->doNextStatus2();
        break;
    case 3:
        this->doNextStatus3();
        break;
    default:
        return;
    }
}

// sub_1000DEA1
void AnalExecutor::doNextStatus0()
{
    int32_t num;
    float slope;

    if (this->checkAndInitCalcUnit())
    {
        this->analInfo.status = 3;
        this->loopCounter0 = 0;
    }
    else if (!this->pEventManager->topOrBottom && this->pEventManager->autoNegative && (slope = this->pEventManager->getNormalizeSlope(), this->pBuffer10Wrapper->hasSalientPointType2(slope)))
    {
        this->initCalcUnit();
        this->analInfo.status = 3;
        this->loopCounter0 = 0;
        if (this->toggle7)
        {
            this->toggle7 = false;
        }
    }
    else
    {
        ++this->loopCounter0;
        if (this->pCaculator->isIndex1Valid_() && this->pCaculator->isIndexPositive() && this->loopCounter0 >= 2)
        {
            this->loopCounter0 = 0;
            if (!this->hasEventType(BASE_TAIL_TANGENT))
            {
                this->pCaculator->setEndValueIfDownSlopeMoreSharp();
            }
        }
        else if (this->isBuffer0TimeExceedEx())
        {
            this->loopCounter0 = 0;
            this->pCaculator->processIndexAndTickmark(-1);
        }
        else if (this->isType2WinH10Exceed())
        {
            this->analInfo.status = 1;
            this->pCaculator->searchIndexAndCalcValueEX();
            this->loopCounter0 = 0;
            if (this->toggle7 == 1)
            {
                num = this->pResInfWrapper->getNum();
                if (!num || this->pCaculator->pTDLLItem->ts > this->pCaculator->pTDLLItem[-1].te)
                {
                    this->toggle7 = false;
                }
            }
        }
        else
        {
            if (this->isType1WinPositive())
            {
                this->analInfo.status = 3;
                if (this->loopCounter0 <= 3 || this->pCaculator->isTypeCode83(-1) || this->pCaculator->isMoreSlope(-1))
                {
                    if (!this->pCaculator->isTHValueInitialized(0))
                    {
                        this->pCaculator->goPrevAfterClear();
                        if (this->pCaculator->isTHValueInitialized(0))
                        {
                            this->pCaculator->setTickmarkStatus(true);
                        }
                    }
                }
                else if (this->pCaculator->isIndexPositive())
                {
                    if (!this->pCaculator->isTHValueInitialized(0))
                    {
                        this->pCaculator->goPrevAfterClear();
                        if (this->pCaculator->isTHValueInitialized(0))
                        {
                            this->pCaculator->setTickmarkStatus(true);
                        }
                    }
                }
                this->loopCounter0 = 0;
                if (this->toggle7 == 1)
                {
                    num = this->pResInfWrapper->getNum();
                    if (!num || this->pCaculator->pTDLLItem->ts > this->pCaculator->pTDLLItem[-1].te)
                    {
                        this->toggle7 = false;
                    }
                }
            }
        }
    }
}

// sub_1000E1A3
void AnalExecutor::doNextStatus1()
{
    float slope;

    if (this->isItem8HsPositive())
    {
        if (!this->pCaculator->getTickmarkStatus())
        {
            this->pCaculator->setFirstApexTick(0);
        }
        this->analInfo.status = this->setItem8AndCalcResult(0);
    }
    else
    {
        if (this->pCaculator->typeCodeStatus)
        {
            goto LABEL_39;
        }
        if (this->isBaseTailTangentEventCountExceed(this->rawbagOffset))
        {
            if (this->pCaculator->isIndex2Valid_())
            {
                this->pCaculator->setTanStopAndIncIndexBuf2(-1);
            }
            this->pCaculator->setIndex1EndResult(-1);
        }
        if (!this->pCaculator->isBuff10HeightExceed() || (this->pCaculator->calcTypeCode(0), !this->pCaculator->getIndex1Flag()) || this->pCaculator->isIndex2Valid_() || this->pCaculator->setPeakTB(0) != 0)
        {
        LABEL_39:
            if (this->isType2Lose1D())
            {
                if (this->pCaculator->typeCodeStatus)
                {
                    this->analInfo.status = 2;
                    this->pCaculator->findAndCalcPeakVaueV2();
                    if (this->pCaculator->checkSlopeIsExceed(15))
                    {
                        this->pCaculator->setTickmarkGoPrev(0);
                    }
                }
                else if (this->pCaculator->isIndexPositive())
                {
                    if (this->pCaculator->isTrEqualTeV1(-1))
                    {
                        this->pCaculator->goPrevAfterClear();
                        this->pCaculator->typeCodeStatus = 1;
                        this->pCaculator->setTickmarkStatus(false);
                    }
                    else
                    {
                        this->pCaculator->calcTypeCode(0);
                        if (this->pCaculator->getIndex1Flag() && !this->pCaculator->isIndex2Valid_())
                        {
                            this->pCaculator->setPeakTB(0);
                        }
                        this->pCaculator->typeCodeStatus = 0;
                        this->pCaculator->setTickmarkStatus(true);
                        this->analInfo.status = 3;
                        this->pCaculator->findAndCalcPeakVaueV2();
                    }
                }
                else
                {
                    this->analInfo.status = 0;
                }
                this->pBuffer10Wrapper->cleanCalcUnit1();
            }
            else if (this->hasEventType(BASE_TOGETHER) || this->hasEventType(BASE_VALLEY) || !this->pCaculator->isBaseIdxGreatWithoutBaseTail() || this->pCaculator->getBaseTailTangant() || !this->isTailModeDHGreaterThenEventWidth())
            {
                if (this->pCaculator->typeCodeStatus)
                {
                    if (this->pEventManager->shouldSens)
                    {
                        slope = this->pEventManager->getNormalizeSlope();
                        if (this->pBuffer10Wrapper->setCalcUnit1(this->pCaculator->pTDLLItem, slope))
                        {
                            this->pCaculator->setAlternateRES();
                            this->pBuffer10Wrapper->cleanCalcUnit1();
                        }
                    }
                }
            }
            else
            {
                this->pCaculator->setBaseTailTangant(true);
                this->pBuffer10Wrapper->cleanCalcUnit1();
            }
        }
    }
}

// sub_1000E4EB
void AnalExecutor::doNextStatus2()
{
    int32_t type1;
    int32_t type2;

    ++this->loopCounter2;
    if (this->item24.hs > 0 && this->calcItemUsingItem24(this->loopCounter2))
    {
        this->analInfo.status = 0;
    }
    else if (this->calcItemUsingItem16(this->loopCounter2))
    {
        this->analInfo.status = 0;
    }
    else
    {
        type2 = this->isType2Win1D();
        type1 = this->isType1Win();
        if (type2)
        {
            this->analInfo.status = 1;
            if (this->loopCounter2 >= 5 && !this->pCaculator->isTypeCode83(0))
            {
                if (this->pCaculator->isDTGreatThenBufferDT())
                {
                    this->pCaculator->isEvSlopeX2GreatThenDH(0);
                }
                else if (this->pCaculator->pTDLLItem->hr - this->pCaculator->pTDLLItem->hs <= 32000.0)
                {
                    this->pCaculator->setTickmarkGoPrev(0);
                }
            }
        }
        else if (type1)
        {
            this->pCaculator->setCurrenPeakValue();
            this->analInfo.status = 3;
            if (this->hasEventType(BASE_MERGE_UPMOST))
            {
                if (this->pCaculator->baseIdx > this->pEventManager->evPara[BASE_MERGE_UPMOST])
                {
                    if ((int32_t)this->pCaculator->pTDLLItem[0].hr >= (int32_t)this->pCaculator->pTDLLItem[-1].hr)
                    {
                        this->pCaculator->pTDLLItem[-1].tr = this->pCaculator->pTDLLItem[0].tr;
                        this->pCaculator->pTDLLItem[-1].hr = this->pCaculator->pTDLLItem[0].hr;
                        this->pCaculator->pTDLLItem[-1].Arear = this->pCaculator->pTDLLItem[0].Arear;
                    }
                    this->pCaculator->pTDLLItem[-1].ts = 0;
                    this->pCaculator->pTDLLItem[-1].hs = 0;
                    this->pCaculator->pTDLLItem[-1].Areas = 0;
                    this->pCaculator->goPrevAfterClear();
                }
            }
        }
        if (type2 || type1)
        {
            this->loopCounter2 = 0;
            this->pBuffer10Wrapper->cleanCalcUnit1();
        }
    }
}

// sub_1000E78A
void AnalExecutor::doNextStatus3()
{
    float fVal;
    int32_t hs;
    int32_t h2;
    int32_t h1;
    int32_t h0;
    int32_t type0;
    int32_t type2;

    if (this->isItem8HsPositive())
    {
        if (this->pCaculator->typeCodeStatus)
        {
            this->analInfo.status = this->calcPeakOrTickmark();
        }
        else
        {
            if (this->pCaculator->isBuff10HeightExceed())
            {
                this->pCaculator->calcTypeCode(0);
            }
        }
        return;
    }
    if (this->pCaculator->getTickmarkStatus())
    {
        if (this->pCaculator->isBuffer10HeightStandout())
        {
            this->pCaculator->finishEndValueAccordingBuffer10(4, 0, 0);
            if (!this->pCaculator->isSlopeX10Exceed(0) || (unsigned char)this->pCaculator->setTickmarkRecursivelyV2(0) == 0)
            {
                this->pCaculator->setTanStopAndIncIndexBuf2(0);
                if (this->pCaculator->movePointerNext())
                {
                    this->pCaculator->setStartFromBuffer10(4, 0);
                    this->pCaculator->clearTrHr();
                }
                if (this->pCaculator->getIndex2Flag())
                {
                    this->pCaculator->setIndex1EndResult(0);
                }
                this->pBuffer10Wrapper->cleanCalcUnit2();
            }
            return;
        }
    }
    else
    {
        if (!this->pCaculator->isTHValueInitialized(0) && this->pCaculator->isEventSlopeExceed() && !this->pCaculator->isIndex2Valid_())
        {
            this->pCaculator->setTickmarkGoPrev(0);
            this->pBuffer10Wrapper->cleanCalcUnit2();
            return;
        }
        if (this->pCaculator->isEvSlopeLitterThenDH())
        {
            this->pCaculator->setFirstApexTick(0);
            if (!this->hasEventType(BASE_TOGETHER) && !this->hasEventType(BASE_VALLEY) && this->pCaculator->isHeightNormal())
            {
                this->pCaculator->isIndexBuf1EqualHead(-1);
                this->pCaculator->setPeakTB(0);
            }
            if (this->pCaculator->isBaseIdxGreatThenIndex1Head())
            {
                if (this->hasEventType(BASE_TAIL_TANGENT) || !this->pCaculator->isCurrentPeakHeightExceed())
                {
                    if (!this->pCaculator->isIndex2Valid_())
                    {
                        this->pCaculator->setPeakTB(0);
                    }
                }
                else
                {
                    this->pCaculator->setSolTickOrIndex2Flag();
                }
            }
        }
    }
    if (this->pEventManager->topOrBottom)
    {
        if (this->checkEmptyEvent(BASE_NEGATIVE))
        {
            hs = (int32_t)this->pCaculator->pTDLLItem->hs;
            h0 = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(0);
            h1 = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(1);
            h2 = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(2);
            if (h0 >= hs && h1 < hs && h2 < h1)
            {
                this->pEventManager->evCount[BASE_NEGATIVE] = this->pBuffer10Wrapper->getBuffer10Time(0);
                this->pEventManager->evType[BASE_NEGATIVE] = 2;
                return;
            }
        }
    }
    this->pCaculator->saveTempValue();
    type2 = this->isType2Win8D();
    type0 = this->isType0Win();
    if (type2 || type0)
    {
        if (this->pCaculator->getBaseTailTangant())
        {
            if (!this->hasEventType(BASE_TOGETHER) && !this->hasEventType(BASE_VALLEY) && this->pCaculator->isHrGreatThenHalfOfFirst())
            {
                this->pCaculator->isIndexBuf1EqualHead(0);
            }
            this->pCaculator->setBaseTailTangant(false);
        }
        this->pBuffer10Wrapper->cleanCalcUnit2();
        if (!this->pCaculator->getTickmarkStatus())
        {
            if (this->pCaculator->isTrEqualTsV1(0))
            {
                this->pCaculator->goPrevAfterClear();
                this->pCaculator->setTickmarkStatus(true);
            }
            else
            {
                this->analInfo.status = 1;
            }
            return;
        }

        this->pCaculator->restoreEndValueFromBuffer10();
        if (this->pEventManager->checkPrgItemEven(this->pCaculator->pTDLLItem->te, 7, NULL, 0))
        {
            this->pCaculator->processIndexAndTickmark(0);
            return;
        }
        if (this->pEventManager->getEventDrift() > 0.0)
        {
            fVal = (float)this->pCaculator->calcSlopeAccordingFTime();
            if (this->pEventManager->getEventDrift() > fVal)
            {
                this->pCaculator->processIndexAndTickmark(0);
                return;
            }
        }
        if ((type2 || type0) && !this->pCaculator->isTHValueInitialized(0))
        {
            if (this->pCaculator->normalTrendNearEnd())
            {
                this->analInfo.status = 3;
                return;
            }
        }
        if (this->pCaculator->isTHValueInitialized(0) || !this->pCaculator->isDriftOrSlopeExceedDeltaH() || (fVal = this->pCaculator->pTDLLItem->hr - this->pCaculator->pTDLLItem->hs, (double)(int32_t)(2 * this->pEventManager->getSlopeX10()) <= fVal) || (BYTE)this->pCaculator->setTickmarkRecursivelyV2(0) == 0)
        {
            if (this->toggle7)
            {
                this->toggle7 = false;
                if (this->pCaculator->lessThenEventDrift())
                {
                    this->pCaculator->processIndexAndTickmark(0);
                    return;
                }
            }
            if (this->pCaculator->movePointerNext())
            {
                if (this->pCaculator->restoreTempEndValue(-1))
                {
                    this->pCaculator->processIndexAndTickmark(-1);
                }
                else if (type2)
                {
                    this->pCaculator->setStartAsPrevEnd();
                    this->analInfo.status = 1;
                }
                else
                {
                    if (this->pCaculator->isIndexPositive())
                    {
                        if (this->pCaculator->isEvWidthX10GreatThenDT(-1))
                        {
                            this->pCaculator->setTickmarkRecursively(0);
                        }
                    }
                    this->analInfo.status = 0;
                }
            }
        }
    }
    else
    {
        if (this->pCaculator->checkTypeCode83AndIndex())
        {
            if (this->pEventManager->shouldSens)
            {
                fVal = this->pEventManager->getNormalizeSlope();
                if (this->pBuffer10Wrapper->setCalcUnit2(this->pCaculator->pTDLLItem, fVal))
                {
                    this->pCaculator->setAlternateERS();
                    this->pBuffer10Wrapper->cleanCalcUnit2();
                }
            }
        }
    }
}

// sub_1000EEAF
int32_t AnalExecutor::checkAndInitCalcUnit()
{
    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return false;
    }
    if (!this->togglerAutoNegative)
    {
        return false;
    }
    this->togglerAutoNegative = false;
    this->initCalcUnit();
    return true;
}

// sub_1000EF06
CBuffer10Wrapper *AnalExecutor::initCalcUnit()
{
    this->pCaculator->searchIndexAndCalcValue();
    this->item8.hs = (float)this->pCaculator->pTDLLItem->hs;
    this->item8.ts = this->pCaculator->pTDLLItem->ts;
    return this->pBuffer10Wrapper->cleanCalcUnit1();
}

// sub_1001B830
int32_t AnalExecutor::isBuffer0TimeExceedEx()
{
    return this->pCaculator->isIndexPositive() && this->loopCounter0 > 8 && this->pCaculator->isBuffer0TimeExceed();
}

// sub_1001B910
int32_t AnalExecutor::isType2WinH10Exceed()
{
    int32_t topBottom;

    topBottom = this->pEventManager->topOrBottom;
    if (this->pBuffer10Wrapper->isDiffH10Exceed(topBottom))
    {
        return true;
    }
    return this->pBuffer10Wrapper->checkType2Counter(topBottom) && (this->pBuffer10Wrapper->checkCurvature(topBottom) || this->pCaculator->isLocalDiffHExtruded1D());
}

// sub_1001BA80
int32_t AnalExecutor::isType1WinPositive()
{
    return this->pCaculator->isIndexPositive() && this->pBuffer10Wrapper->checkType1Counter(this->pEventManager->topOrBottom);
}

// sub_1001BC50
int32_t AnalExecutor::isBaseTailTangentEventCountExceed(int32_t threshold)
{
    if (this->pEventManager->evCount[BASE_TAIL_TANGENT] <= 0 || threshold < this->pEventManager->evCount[BASE_TAIL_TANGENT])
    {
        return false;
    }
    this->pEventManager->evCount[BASE_TAIL_TANGENT] = 0;
    return true;
}

// sub_1001BD00
int32_t AnalExecutor::isTailModeDHGreaterThenEventWidth()
{
    int32_t evWidth;
    int32_t result;

    result = false;
    if (this->pEventManager->tailMode)
    {
        evWidth = this->pEventManager->eventWidth;
        if (this->pBuffer10Wrapper->isDiffHeightExceedThreshold(evWidth))
        {
            result = true;
        }
    }
    return result;
}

// sub_1000F37A
int32_t AnalExecutor::calcItemUsingItem24(int32_t offset)
{
    float slope;
    int32_t result;

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return false;
    }
    if (offset < 3 && !this->isType2Win1D() && !this->isType1Win())
    {
        return false;
    }
    this->item24.tr = (float)this->pBuffer10Wrapper->getBuffer10Time(0);
    this->item24.hr = (float)this->pBuffer10Wrapper->getBuffer10Height(0);
    this->item24.arear = this->pBuffer10Wrapper->getBuffer10Area(0);
    slope = this->pEventManager->getSlope();
    if (this->checkAreaDeviation(this->pCaculator->pTDLLItem, &this->item24, slope))
    {
        result = this->pCaculator->setPeakEndTypeNB(this->item24.tr, this->item24.hr, this->item24.arear) != false;
    }
    else
    {
        result = false;
    }
    this->clearItem24();
    return result;
}

// sub_1000F49D
int32_t AnalExecutor::calcItemUsingItem16(int32_t offset)
{
    int32_t result;

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return false;
    }
    if (offset < 3 && !this->isType2Win1D() && !this->isType1Win())
    {
        return false;
    }
    this->item16.tr = (float)this->pBuffer10Wrapper->getBuffer10Time(0);
    this->item16.hr = (float)this->pBuffer10Wrapper->getBuffer10Height(0);
    this->item16.arear = this->pBuffer10Wrapper->getBuffer10Area(0);
    result = this->pCaculator->setPeakAllTypeNB((int32_t)this->item16.tr, this->item16.hr, this->item16.arear) != 0;
    this->clearItem16();
    return result;
}

// sub_1001BEF0
int32_t AnalExecutor::isType2Win1D()
{
    return this->pBuffer10Wrapper->checkType2Counter(this->pEventManager->topOrBottom) && this->pCaculator->isLocalDiffHExtruded1D();
}

// sub_1001BF50
int32_t AnalExecutor::isType1Win()
{
    return this->pBuffer10Wrapper->checkType1Counter(this->pEventManager->topOrBottom);
}

// sub_1000F585
int32_t AnalExecutor::calcPeakOrTickmark()
{
    int32_t result; // eax

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return 3;
    }
    if (!this->isType2Win8D())
    {
        return 3;
    }
    if (this->pCaculator->isDownSlopeExceedEvDrift(0))
    {
        this->pCaculator->findAndCalcPeakVaue();
        result = 1;
    }
    else
    {
        this->item8.hs = 0;
        this->pCaculator->setTickmarkGoPrev(0);
        result = 0;
    }
    return result;
}

// sub_1001C430
int32_t AnalExecutor::isType2Win8D()
{
    return this->pBuffer10Wrapper->checkType2Counter(this->pEventManager->topOrBottom) && this->pCaculator->isLocalDiffHExtruded8D();
}

// sub_1001C500
int32_t AnalExecutor::isType0Win()
{
    return this->pBuffer10Wrapper->checkType0Counter() && this->pCaculator->isLocalDiffHExtruded16D_();
}

// sub_1001632B
int32_t AnalExecutor::checkPeakAndCalcMetricV1(float beginTime, float endTime, float, int32_t flag)
{
    int32_t result;
    int32_t resCount;
    int32_t head;
    int32_t tail;
    TDLLResultListItem *pResItems;
    float freq;

    pResItems = (TDLLResultListItem *)this->pResInfWrapper->head;
    resCount = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pResItems || !resCount || !this->pRawBagBuffer || !freq)
    {
        return false;
    }
    if (checkTrPeak(&head, &tail, beginTime, endTime, pResItems, resCount, freq))
    {
        if (!flag)
        {
            this->calcAndSetMetric(head, tail, NULL);
        }
        result = true;
    }
    else
    {
        this->setError(flag, aThereIsNtAnyPe7_CN, aThereIsNtAnyPe7_EN);
        result = false;
    }
    return result;
}

// sub_10016255
int32_t AnalExecutor::checkPeakAndCalcMetricV2(float beginTime, float endTime, float, int32_t flag)
{
    int32_t result;
    int32_t resCount;
    int32_t head;
    int32_t tail;
    TDLLResultListItem *pResItems;
    float freq;

    pResItems = (TDLLResultListItem *)this->pResInfWrapper->head;
    resCount = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pResItems || !resCount || !this->pRawBagBuffer || !freq)
    {
        return false;
    }
    if (checkTrPeak(&head, &tail, beginTime, endTime, pResItems, resCount, freq))
    {
        if (!flag)
        {
            this->calcAndSetMetric(head, tail, NULL);
        }
        result = true;
    }
    else
    {
        this->setError(flag, aThereIsNtAnyPe6_CN, aThereIsNtAnyPe6_EN);
        result = false;
    }
    return result;
}

// sub_100160A9
int32_t AnalExecutor::checkPeakAndCalcMetricV3(float beginTime, float endTime, int32_t flag)
{
    int32_t result;
    int32_t resCount;
    int32_t head;
    int32_t tail;
    TDLLResultListItem *pResItems;
    float freq;

    pResItems = (TDLLResultListItem *)this->pResInfWrapper->head;
    resCount = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pResItems || !resCount || !this->pRawBagBuffer || !freq)
    {
        return false;
    }
    if (checkTrPeak(&head, &tail, beginTime, endTime, pResItems, resCount, freq))
    {
        if (!flag)
        {
            this->calcAndSetMetric(head, tail, NULL);
        }
        result = true;
    }
    else
    {
        this->setError(flag, aThereIsNtAnyPe4_CN, aThereIsNtAnyPe4_EN);
        result = false;
    }
    return result;
}

// sub_1001617F
int32_t AnalExecutor::checkPeakAndCalcMetricV4(float beginTime, float endTime, int32_t flag)
{
    int32_t result;
    int32_t resCount;
    int32_t head;
    int32_t tail;
    TDLLResultListItem *pResItems;
    float freq;

    pResItems = (TDLLResultListItem *)this->pResInfWrapper->head;
    resCount = this->pResInfWrapper->getNum();
    freq = this->pTsmpBag->getRawBagFreqValue();
    if (!pResItems || !resCount || !this->pRawBagBuffer || !freq)
    {
        return false;
    }
    if (checkTrPeak(&head, &tail, beginTime, endTime, pResItems, resCount, freq))
    {
        if (!flag)
        {
            this->calcAndSetMetric(head, tail, NULL);
        }
        result = true;
    }
    else
    {
        this->setError(flag, aThereIsNtAnyPe5_CN, aThereIsNtAnyPe5_EN);
        result = false;
    }
    return result;
}

// sub_1001D860
int32_t AnalExecutor::setUkPkFactor(TUkPkFactor *lpUkPkFactor)
{
    if (!this->pConcCaculator)
    {
        return false;
    }
    memset((void *)&this->pConcCaculator->tukpkFactor, 0, sizeof(TUkPkFactor));
    if (lpUkPkFactor)
    {
        memcpy((void *)&this->pConcCaculator->tukpkFactor, (void *)lpUkPkFactor, sizeof(TUkPkFactor));
    }
    return true;
}

// sub_1001D8C0
int32_t AnalExecutor::setIdtTimeOfs(float fTimeOfs)
{
    int32_t result;

    if (this->pConcCaculator)
    {
        result = this->pConcCaculator->setIdtTimeOfs(fTimeOfs);
    }
    else
    {
        result = false;
    }
    return result;
}

// sub_1001D920
int32_t AnalExecutor::setYMinUnit(double yMinUnit)
{
    double unit;

    if (!this->pConcCaculator)
    {
        return false;
    }
    if (yMinUnit <= 1.0e-30)
    {
        unit = -1.0;
    }
    else
    {
        unit = yMinUnit;
    }
    this->pConcCaculator->yMinUnit = unit;
    return true;
}

// sub_1001D990
int32_t AnalExecutor::setIdtQualitMode(int32_t nQualitMode, float fTimeWindow)
{
    int32_t result;

    if (this->pConcCaculator)
    {
        result = this->pConcCaculator->setIdtQualitMode(nQualitMode, fTimeWindow);
    }
    else
    {
        result = false;
    }
    return result;
}

// sub_1001DA20
int32_t AnalExecutor::setXMinUnitK(double xMinUnitK)
{
    if (!this->pConcCaculator)
    {
        return false;
    }
    if (xMinUnitK <= 1.0e-30)
    {
        xMinUnitK = 60.0;
    }
    this->pCaculator->xMinUnitK = xMinUnitK;
    this->pResInfWrapper->xMinUnitK = xMinUnitK;
    return true;
}

// sub_1001DA90
int32_t AnalExecutor::setMiscellaneous(TMiscellaneous *wParam, int64_t)
{
    int32_t byCalib;
    int32_t result = false;

    if (this->pConcCaculator)
    {
        if (this->pTsmpBag && this->pTsmpBag->metric)
        {
            byCalib = this->pTsmpBag->metric->byCalib;
        }
        else
        {
            byCalib = false;
        }
        result = this->pConcCaculator->setMiscellaneous(wParam, byCalib);
    }
    if (this->pCaculator)
    {
        result = this->pCaculator->setXMinUnitK(wParam);
    }
    return result;
}

// sub_1001D800
CTickmarkManager *AnalExecutor::callbackDraw(int32_t chn)
{
    return this->pCTickmarkManager->callbackDrawTickMark(chn);
}
